首页 > 其他 > 详细

51nod 1127 最短的包含字符串(尺取法)

时间:2017-11-23 11:37:11      阅读:305      评论:0      收藏:0      [点我收藏+]

题目:

技术分享图片

 

 

 

简单尺取法

代码:

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <math.h>
#include <queue>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
typedef long long ll;
#define INF 2147483647

string s; 
map <char,int> m;

int main() {
    cin >> s;
    int len = s.length();
    int l = 0,r = 0;int num = 0;
    int ans = 2000000000;
    while(true){
        while(num < 26 && r < len){
            if(m[s[r]] == 0) num++;
            m[s[r]]++;
            r++;
        }
        if(num < 26) break;
        while(num == 26 && l < r){
            if(m[s[l]] == 1) num--;
            m[s[l]]--;
            l++;
        }
        ans = min(ans,r-l+1);
    }
    if(ans == 2000000000) cout << "No Solution" << endl;
    else cout << ans << endl;
    return 0;
}

 

51nod 1127 最短的包含字符串(尺取法)

原文:http://www.cnblogs.com/zhangjiuding/p/7883972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!