「两个指针的应用」最短摘要的生成
2015-09-12 10:34
309 查看
/************************************************************************* > 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; }
相关文章推荐
- android selector(转)
- 数据库心得体会
- Delphi和JAVA用UTF-8编码进行Socket通信例子
- javascript判断网页是关闭还是刷新
- 个人学习笔记---linux内核同步
- 以实例讲解Objective-C中的KVO与KVC机制
- 数据挖掘(9):BP神经网络算法与实践
- Delphi XE4,C++ Builder XE4,RAD Studio XE4 v18.0.4905.60485(With Update 1) 官方下载激活
- Remove Element
- Über-shader与deferred shading
- 软件工程的实践项目的自我目标
- Invoke
- 【hdu5304】生成树计数—基尔霍夫矩阵 DP
- CSS小贴士(垂直居中和百分比下调整盒模型填充)
- 数据挖掘(8):朴素贝叶斯分类算法原理与实践
- 位运算及其应用详解
- Remove Duplicates from Sorted Array
- 内核之旅 --- 内核模块学习1---内核模块参数传递
- 内核之旅 --- 内核模块学习1---内核模块参数传递
- 深入解析字符串的比较方法:“==”操作符;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。