编程之美之最短摘要生成的困惑

来源:互联网 发布:中国强大知乎 编辑:程序博客网 时间:2024/05/01 16:35
// 编程之美之最短摘要的生成.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <string>#include <iostream>#include <queue>#define N 3using 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 elementj=que[i].front();//update jcout<<"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;}
上面是用堆栈的方法,感觉时间复杂度不是很好(主要在处理堆栈的问题上)。编程之美书上的第二种方法没有考虑到查找所有的关键字的时间。如何查找所有的关键字呢,在百度上搜了一下,没有几个给出方法的,待完成。。。
原创粉丝点击