1. 程式人生 > >51nod 1127 最短的包含字符串

51nod 1127 最短的包含字符串

har pre 如果 algorithm \n tail nbsp algo fine

給出一個字符串,求該字符串的一個子串s,s包含A-Z中的全部字母,並且s是所有符合條件的子串中最短的,輸出s的長度。如果給出的字符串中並不包括A-Z中的全部字母,則輸出No Solution。 Input
第1行,1個字符串。字符串的長度 <= 100000。
Output
輸出包含A-Z的最短子串s的長度。如果沒有符合條件的子串,則輸出No Solution。
Input示例
BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ
Output示例
28

尺取。
代碼:
#include <iostream>
#include 
<cstdio> #include <algorithm> #define MAX 100001 using 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 最短的包含字符串