// 编程之美之最短摘要的生成.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <queue>
#define N 3
using namespace std;
//该算法对每个关键字出现的位置进行入队列操作O(m*n),然后操作队列O(n*length(第一个队列的长度)),队列处理应该仍然可以进行优化,怎优化呢?
//
int _tmain(int argc, _TCHAR* argv[])
{
string text="hello shi ! creek hello, you are such a stupid little boy, hello ,creek, but you have a good family name shi, do you know?";
string query[N] = {"hello","creek","shi"};
queue<int> que[N];
for(int i=0;i<N;i++)
{
int position=0;
while((position=text.find(query[i],position))!=string::npos)
{
que[i].push(position);
position++;
cout<<"here "<<endl;
}
cout<<"here "<<endl;
}
//system("pause");
int sum=10000;
int startpoint,endpoint;
while(!que[0].empty())
{
int sum1=que[0].front(),sum2=0;//sum1用于计算最短长度,表示当前关键字序列的起始位置
int j =sum1;//j表示当前的大小
que[0].pop();
cout<<"here sum1 is "<<sum1<<endl;
for(int i=1;i<N;i++)
{
while(!que[i].empty()&&que[i].front()<j)que[i].pop();//find the first one who is greater than last queue element
j=que[i].front();//update j
cout<<"here j is "<<j<<endl;
}
sum2=j;
if(sum>sum2-sum1){
sum=sum2-sum1;
startpoint=sum1;
endpoint=sum2;
}
}
cout<<"startpint is "<<startpoint<<" endpoint is "<<endpoint<<" shortest sum is "<<sum<<endl;
system("pause");
return 0;
}