ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than26, no such substring exists and thus it is not nice.
Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him?
The first and only line of the input contains a single string s (1 ≤ |s| ≤ 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember.
If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print - 1 in the only line.
Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.
If there are multiple solutions, you may print any of them.
ABC??FGHIJK???OPQR?TUVWXY?
ABCDEFGHIJKLMNOPQRZTUVWXYS
WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO
-1
??????????????????????????
MNBVCXZLKJHGFDSAQPWOEIRUYT
AABCDEFGHIJKLMNOPQRSTUVW??M
-1
In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such asABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.
In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length26 that contains all the letters of the alphabet, so the answer is - 1.
In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer.
题目大意:把所给的字符串中的“?”用A->Z来代替;如果能则输出替换之后的字符串,不能就输出-1.
思路:运用尺取法每次截取一个长度为26的子字符串
代码如下:
1 #include2 #include 3 4 const int Max=5e4+5; 5 char S[Max]; 6 int P[30]; 7 8 int main(){ 9 while(~scanf("%s",S)){10 bool flag=true;11 int len=strlen(S);12 if(len<26){ //长度短于26的直接pass;13 printf("-1\n");14 continue;15 }16 for(int i=0;i<=len-26 && flag;i++){17 int cnt1=0,cnt2=0;18 memset(P,0,sizeof(P));19 for(int j=i;j ='A'&&S[j]<='Z'){21 P[S[j]-'A']++; //记录该子字符串里面有多少个字母;22 }23 else{24 cnt2++; //记录该子字符串里有多少个?;25 }26 }27 for(int j=0;j<26;j++){28 if(P[j]==1) cnt1++; //记录子字符串里每个字母出现的次数;29 }30 if(cnt1+cnt2 == 26){ //判断是否能有一个子字符串能够满足题意;31 int t=0;32 for(int j=i;j