51nod 1127 最短的包含字符串

给一串字符串,求最短的包含‘A’-‘Z’字母的子串。

尺取。

#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
string s;
int cnt[26];
int ans = inf;
inline bool check() {
    for (int i = 0; i < 26; i++)
        if (!cnt[i]) return 0;
    return 1;
}

int main() {
    cin >> s;
    int l = 0, r = 0;
    while (r < s.size()) {
        cnt[s[r++]-'A']++;
        while (check()){
            cnt[s[l++]-'A']--;
            ans=min(ans,r-l+1);
            if(!check()){
                cnt[s[--l]-'A']++;
                break;
            }
        }
    }
    if(ans==inf) cout <<"No Solution\n";
    else cout << ans;
    return 0;
}
posted @ 2020-07-14 20:26  Mr.doublerun  阅读(20)  评论(0)    收藏  举报