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

// 编程之美之最短摘要的生成.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;
}
上面是用堆栈的方法,感觉时间复杂度不是很好(主要在处理堆栈的问题上)。编程之美书上的第二种方法没有考虑到查找所有的关键字的时间。如何查找所有的关键字呢,在百度上搜了一下,没有几个给出方法的,待完成。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值