B. Complete the Word
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 than 26, 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
思路:思路就是没什么思路。。。纯模拟,怎么考虑呢,我是限定一个长26的框,第一次限定0-25,用v[26]记录每个大写字母出现的个数,26个字母里有几个?代表需要几个没用的字母填充,我用need代表,have表示v[26]中几个为0,即没有被用。
然后怎么办呢?遍历开头,第一次特判,然后从1到s.length-26,每次减去i-1位置的数据(主要是v[s[i]],have和need),当need==have说明匹配了,输出,break即可。
反正过程有许多小细节吧!练练手!
#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
int v[26],pos;
int main()
{
input;
string s,s1;
int need,have,flag;
int i,j;
while(cin>>s){
s1="";
flag=0;
memset(v,0,sizeof(v));
if(s.length()<26){ //如果比26还短是不可能的
cout<<-1<<endl;
continue;
}
have=26;
need=0;
for(i=0;i<26;i++){ //初始化have,need和v[i]
if(s[i]=='?')need++;
else{
if(v[s[i]-'A']==0)have--;
v[s[i]-'A']++;
}
}
if(have==need) //第一次特判了,即第一次就匹配到了
{
int pos=0;
for(i=0;i<26;i++) { //这里是从0-25后面就不是了!
if(s[i]!='?') s1+=s[i];
else{
while(v[pos]){
if(pos>25)break;
pos++;
}
s1+=('A'+pos);
v[pos]=1;
}
}
for(i=26;i<s.length();i++){ //处理匹配的位置其他字段也要写入!
if(s[i]!='?') s1+=s[i];
else s1+='A';
}
cout<<s1<<endl;
continue;
}
for(i=1;i<=s.length()-26;i++){ //遍历开头
if(s[i-1]=='?') need--; //处理开头
else{
v[s[i-1]-'A']--;
if(v[s[i-1]-'A']==0) have++;
}
if(s[i+25]=='?') need++; //处理末尾加入的
else {
if(v[s[i+25]-'A']==0)have--;
v[s[i+25]-'A']++;
}
if(s[i-1]=='?') s1+='A'; //记得把前一个没有的填充了
else s1+=s[i-1];
if(need==have){
int pos=0;
for(int k=i;k<i+26;k++){ //这里开头变了!
if(s[k]!='?') s1+=s[k];
else{
while(v[pos]){
if(pos>25)break;
pos++;
}
v[pos]=1;
s1+=('A'+pos);
}
}
for(i=i+26;i<s.length();i++){
if(s[i]!='?') s1+=s[i];
else s1+='A';
}
cout<<s1<<endl;
flag=1;
}
if(flag==1)break;
}
if(flag==0)cout<<-1<<endl;
}
return 0;
}