「两个指针的应用」最短摘要的生成

/*************************************************************************
	> 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值