51NOD 1127 最短的包含字符串

传送门

给出一个字符串,求该字符串的一个子串S,S包含A-Z中的全部字母,并且S是所有符合条件的子串中最短的,输出S的长度。如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。
Input
第1行,1个字符串。字符串的长度 <= 100000。
Output
输出包含A-Z的最短子串长度。如果没有符合条件的子串,则输出No Solution。
Input示例
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output示例
28

解题思路:
借助尺取法。

My Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int MAXN = 1e5+5;
char str[MAXN];
int sum[30];
int main()
{
    while(~scanf("%s",str))
    {
        memset(sum , 0, sizeof(sum));
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            if(str[i]<='Z' && str[i]>='A')
                sum[str[i]-'A']++;
        }
        bool ok = 0;
        for(int i=0; i<26; i++)
            if(!sum[i])
            {
                puts("No Solution");
                ok = 1;
                break;
            }
        if(ok)
            continue;
        memset(sum, 0, sizeof(sum));
        int l = 0, r = 0, cnt = 0, Min = 9999999;
        while(r<len || cnt==26)
        {
            while(r<len && cnt<26)
            {
                if(!sum[str[r]-'A'])
                {
                    cnt++;
                }
                sum[str[r]-'A']++;
                r++;
            }
            while(sum[str[l]-'A'] > 1)
            {
                sum[str[l]-'A']--;
                l++;
            }
            if(cnt == 26)
                Min = min(Min, r-l);
            cnt--;
            sum[str[l++]-'A']--;
        }
        cout<<Min<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值