/*************************************************************************
> File Name: ShortestContaining.cpp
> Author: Shaojie Kang
> Mail: kangshaojie@ict.ac.cn
> Created Time: 2015年09月12日 星期六 09时48分25秒
> Problem:
最短摘要的生成
************************************************************************/
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
// Initialize
string strs1[] = {
"obvious", "hello", "problem", "perseverance", "congratulations", "ok", "kang", "jiayou", "hello", "couragek"
};
vector<string> source(strs1, strs1 + sizeof(strs1)/sizeof(string));
string strs2[] = {
"hello", "kang"
};
map<string, bool> dict;
for(int i = 0; i < sizeof(strs2)/sizeof(string); ++i)
dict[strs2[i]] = true;
// Definition
map<string , int> found;
int sizeSource = source.size();
int sizeDict = dict.size();
int count = 0;
int targetBegin = 0;
int targetEnd = sizeSource;
int begin = -1;
int end = -1;
while(end < sizeSource)
{
while(end < sizeSource && count < sizeDict)
{
end++;
if(end < sizeSource && dict.find(source[end]) != dict.end())
{
found[source[end]]++;
if(found[source[end]] == 1)
count++;
}
}
if(count == sizeDict)
{
while(begin < end)
{
begin++;
if(dict.find(source[begin]) != dict.end())
{
found[source[begin]]--;
if(found[source[begin]] == 0)
{
count--;
break;
}
}
}
if(end - begin < targetEnd - targetBegin)
{
targetBegin = begin;
targetEnd = end;
cout<<targetBegin<<":"<<targetEnd<<endl;
}
}
cout<<"end:"<<end<<endl;
}
for(int i = targetBegin; i <= targetEnd; ++i)
cout<<source[i]<<" ";
cout<<endl;
return 0;
}
「两个指针的应用」最短摘要的生成
最新推荐文章于 2023-06-22 14:13:02 发布