最短摘要生成算法

int FindAbstract(vector<string>&sent,vector<string> &keys,vector<string> &rst)
{
	map<string,int> keyMp;
	set<string> keySt;
	int beg = 0,end = 0,s = 0,t = 0,MinLen = 0x7fffffff;
	queue<int> pos;//将所有关键词的下标放入队列中,当关键词的种类全时,就要从最开始的下标开始遍历
	
	for(int i=0;i<keys.size();++i)
		keySt.insert(keys[i]);

	for (;end<sent.size();++end)
	{
		if (keySt.find(sent[end])!=keySt.end())
		{
			keyMp[sent[end]] +=1;
			pos.push(end);

			while (keyMp.size() == keys.size())
			{
				beg = pos.front();
				pos.pop();
				keyMp[sent[beg]] -=1;
				if(keyMp[sent[beg]] == 0)
					keyMp.erase(sent[beg]);
				int span = end - beg +1;
				if (span < MinLen)
				{
					MinLen = span;
					s = beg;
					t = end;
				}
			}
		}
	}

	for (int i=s;i<=t;++i)
		rst.push_back(sent[i]);
	return MinLen;
}
int main()
{
	string strarr[] = {"d","b","e","c","a","b","e","a","e","b","d"};
	vector<string> cnt;
	for(int i=0;i<11;++i)
		cnt.push_back(strarr[i]);

	vector<string> keys;
	keys.push_back("a");
	keys.push_back("d");
	keys.push_back("e");
	vector<string> rst;
	cout<<FindAbstract(cnt,keys,rst)<<endl;
	for(int i=0;i<rst.size();++i)
		cout<<rst[i]<<" ";
	return 0;
}

你可能感兴趣的:(c,算法,String)