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

来源:互联网 发布:高分一号数据操作教程 编辑:程序博客网 时间:2024/06/06 11:00

经典尺取法

#include <bits/stdc++.h>using namespace std;const int MAXN = 100010;char str[MAXN];int mark[26];bool check(){    int cnt = 0;    for(int i = 0; i < 26; ++i)        if(mark[i] > 0)            ++cnt;    if(cnt == 26)        return true;    else        return false;}int main(){    scanf("%s",str);    int len = strlen(str);    int s = 0,e = 0,nl = 0;    int res = MAXN;    while(e < len)    {        if(check())        {            if(nl < res)                res = nl;            --mark[str[s++]-'A'];            --nl;        }        else        {            ++mark[str[e++]-'A'];            ++nl;        }    }    if(res == MAXN)        printf("No Solution\n");    else        printf("%d\n",res);    return 0;}