[cpp]
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define KeyWordNumber 3
bool isAllExisted(string s1, string s2[])
{
for (int i = 0; i < KeyWordNumber; ++i)
{
if (s1.find(s2[i]) == string::npos) return 0;
}
return 1;
}
string ExtractSummary(string description, string keywords[])
{
int nTargetLen = description.length();
//设置最大目标长度
int pBegin = 0;
//初始指针
int pEnd = 0;
//结束指针
int nAbstractBegin = 0;
//目标摘要的起始地址
int nAbstractEnd = 0;
//目标摘要的结束地址
while (1)
{
//假设没有包含所有的关键词,并且后面的指针没有越界,往后移动指针
while (pEnd < description.length()-1 && !isAllExisted(description.substr(pBegin, pEnd - pBegin + 1), keywords))
{
pEnd++;
}
//假设找到一段包含所有关键信息的字符串
while (isAllExisted(description.substr(pBegin, pEnd - pBegin + 1), keywords))
{
if (pEnd - pBegin + 1 <= nTargetLen)
{
nTargetLen = pEnd - pBegin + 1;
nAbstractBegin = pBegin;
nAbstractEnd = pEnd;
}
pBegin++;
}
if (pEnd >= description.length()-1) break;
}
return description.substr(nAbstractBegin, nAbstractEnd - nAbstractBegin + 1);
}
int main()
{
string text = "money is important than knife and money is can buy garden";
string abstract[3];
abstract[0] = "money";
abstract[1] = "and";
abstract[2] = "garden";
string res = ExtractSummary(text, abstract);
cout << res << endl;
return 0;
}