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

题目:

简单尺取法

代码:

#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;
}
原文地址:https://www.cnblogs.com/zhangjiuding/p/7883972.html