给出一个字符串,求该字符串的一个子串s,s包含A-Z中的全部字母,并且s是所有符合条件的子串中最短的,输出s的长度。如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution。
收起
输入
第1行,1个字符串。字符串的长度 <= 100000。
输出
输出包含A-Z的最短子串s的长度。如果没有符合条件的子串,则输出No Solution。
输入样例
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
输出样例
28
利用数组进行标记,因为只统计字母,于是放弃了大小写,进而开一个标记数组,用两个维护头和尾,这样中间的距离不断的被更新,就能得到最大的长度
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<vector>
using namespace std;
int a[100];
char s[100100];
int check()
{
for(int i=0; i<26; i++)
{
if(a[i]==0)
return 0;
}
return 1;
}
int main()
{
scanf("%s",s);
int len=strlen(s);
int ans=0x3f3f3f3f;
int l=0;
int r=25;
for(int i=l; i<=r; i++)
{
int t=s[i]-'A';
a[t]++;
}
while(l<=len-26&&r<len)
{
while(!check()&&r<len-1)
{
a[s[++r]-'A']++;
}
if(check())
{
ans=min(ans,r-l+1);
}
a[s[l]-'A']--;
l++;
}
if(ans!=0x3f3f3f3f)
printf("%d\n",ans);
else
printf("No Solution\n");
return 0;
}