B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法

时间:2023-03-09 08:41:54
B. Complete the Word(Codeforces Round #372 (Div. 2))   尺取大法
B. Complete the Word
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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?

Input

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.

Output

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.

Examples
Input
ABC??FGHIJK???OPQR?TUVWXY?
Output
ABCDEFGHIJKLMNOPQRZTUVWXYS
Input
WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO
Output
-1
Input
??????????????????????????
Output
MNBVCXZLKJHGFDSAQPWOEIRUYT
Input
AABCDEFGHIJKLMNOPQRSTUVW??M
Output
-1
Note

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的子字符串

代码如下:

 #include <cstdio>
#include <cstring> const int Max=5e4+;
char S[Max];
int P[]; int main(){
while(~scanf("%s",S)){
bool flag=true;
int len=strlen(S);
if(len<){ //长度短于26的直接pass;
printf("-1\n");
continue;
}
for(int i=;i<=len- && flag;i++){
int cnt1=,cnt2=;
memset(P,,sizeof(P));
for(int j=i;j<i+;j++){ //尺取大法;
if(S[j]>='A'&&S[j]<='Z'){
P[S[j]-'A']++; //记录该子字符串里面有多少个字母;
}
else{
cnt2++; //记录该子字符串里有多少个?;
}
}
for(int j=;j<;j++){
if(P[j]==) cnt1++; //记录子字符串里每个字母出现的次数;
}
if(cnt1+cnt2 == ){ //判断是否能有一个子字符串能够满足题意;
int t=;
for(int j=i;j<i+;j++){ //将符合条件的子字符串中的?号换成字母;
if(S[j]=='?'){
for(;t<;t++){
if(P[t]==){
S[j]='A'+t;
t++;
break;
}
}
}
}
flag=false; //只需一组即可;
}
}
for(int i=;i<len;i++){ //剩余的问号随便用字母代替;
if(S[i]=='?'){
S[i]='A';
}
}
if(flag) printf("-1\n");
else printf("%s\n",S);
}
}