Wannafly挑战赛23 A字符串 尺取法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/82284943

Description

S 26 给出一个字符串S,保证存在一个子串满足其中包含了所有的26个字母。找出长度最短的子串。


Input

S 1 0 5 |S| \leq 10^5


Output

子串的长度


Solution

尺取法。


Codes

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int vis[30];
char str[maxn];
int main()
{
    while(~scanf("%s",str)) {
        memset(vis,0,sizeof(vis));
        int res = 0x3f3f3f3f,cnt = 0;
        int len = strlen(str);
        for(int i=0,j=0;;) {
            while(i < len && cnt < 26) {
                if(!vis[str[i]-'a']) {
                    cnt++;
                }
                vis[str[i]-'a']++;
                i++;
            }
            if(cnt < 26) break;
            res = min(res,i-j);
            if(vis[str[j]-'a']) vis[str[j]-'a']--;
            if(vis[str[j]-'a'] == 0) cnt--;
            j++;
        }
        printf("%d\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/82284943
今日推荐