Returns the length of a substring S, which contains all the letters in the A-Z, and s is the shortest length of output s for all qualified substrings. If the given string does not include all the letters in the A-Z, no solution is output. Input
1st rows, 1 string. The length of the string <= 100000.
Output
Output the length of the shortest substring s containing the A-Z. If no sub-string meets the condition, no solution is output.
Input example
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output example
28
Ruler.
Code:
#include <iostream>#include <cstdio>#include <algorithm>#define MAX 100001using namespace std;char s[MAX];int m = MAX;int main() { int head = 0,tail = 0,c = 0,num[26] = {0}; scanf("%s",s); while(s[tail] || c == 26) { if(c < 26) { int d = s[tail ++] - ‘A‘; if(!num[d]) c ++; num[d] ++; } else { m = min(m,tail - head); int d = s[head ++] - ‘A‘; if(num[d] == 1) c --; num[d] --; } } if(m != MAX)printf("%d\n",m); else printf("No Solution\n");}
51nod 1127 contains the shortest string