Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Wordtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputZS 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?
InputThe 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.
OutputIf 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.
ExamplesinputABC??FGHIJK???OPQR?TUVWXY?outputABCDEFGHIJKLMNOPQRZTUVWXYSinputWELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWOoutput-1input??????????????????????????outputMNBVCXZLKJHGFDSAQPWOEIRUYTinputAABCDEFGHIJKLMNOPQRSTUVW??Moutput-1NoteIn the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (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 as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.
In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 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.
题目链接:
http://codeforces.com/contest/716/problem/B
题目大意:
给你一个长度为N(N<=100000)只含大写字母和'?'的字符串,其中'?'可以被任意大写字母替换,问这个字符串是否能够含有一个连续的长度为26的子串,使得每个大写字母只出现一次。
题目思路:
【模拟】
枚举左右端点I和J,并且记录最后一个大写字母出现的位置,如果这个字母已经出现过并且最后出现的位置X在I之后,那么直接把I移动到X+1,继续做,直到I~J的长度为26
接下来对于'?'只要从I到J依次填入没出现过的字母,其余不在I~J的随意填。
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define abs(a) ((a)>0?(a):(-(a))) 23 #define lowbit(a) (a&(-a)) 24 #define sqr(a) ((a)*(a)) 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define eps (1e-10) 28 #define J 10000 29 #define mod 1000000007 30 #define MAX 0x7f7f7f7f 31 #define PI 3.14159265358979323 32 #pragma comment(linker,"/STACK:1024000000,1024000000") 33 #define N 50004 34 using namespace std; 35 typedef long long LL; 36 double anss; 37 LL aans; 38 int cas,cass; 39 int n,m,lll,ans; 40 char s[N]; 41 int mark[30]; 42 int main() 43 { 44 #ifndef ONLINE_JUDGEW 45 freopen("1.txt","r",stdin); 46 // freopen("2.txt","w",stdout); 47 #endif 48 int i,j,k; 49 int x,y,z; 50 // init(); 51 // for(scanf("%d",&cass);cass;cass--) 52 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 53 while(~scanf("%s",s)) 54 // while(~scanf("%d",&n)) 55 { 56 n=strlen(s); 57 mem(mark,-1); 58 if(n<26){puts("-1");continue;} 59 for(i=0,j=0;j<n;j++) 60 { 61 if(s[j]=='?') 62 { 63 if(j-i==25)break; 64 continue; 65 } 66 k=s[j]-'A'; 67 if(mark[k]!=-1) 68 { 69 i=max(i,mark[k]+1); 70 mark[k]=j; 71 } 72 else mark[k]=j; 73 if(j-i==25)break; 74 } 75 if(j==n || j-i<25)puts("-1"); 76 else 77 { 78 x=0; 79 for(k=i;k<=j;k++) 80 { 81 if(s[k]=='?') 82 { 83 for(;x<26 && mark[x]>=i;x++); 84 s[k]=x+'A';mark[x]=i; 85 } 86 } 87 for(i=0;i<n;i++) 88 if(s[i]=='?')s[i]='A'; 89 puts(s); 90 } 91 } 92 return 0; 93 } 94 /* 95 // 96 97 // 98 */