Codeforces Round #372 (Div. 2) B. Complete the Word __ two pointers、队列(queue)
2016-09-23 19:06
501 查看
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 26where 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
output
input
output
input
output
input
output
Note
In 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 asABCDEFGHIJKLMNOPQRSTUVWXYZ 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.
Source
Codeforces Round #372 (Div. 2)
My Solution
two pointers、队列(queue)
用 queue<char> que;维护一个除了 '?'以外所有字符最多在该队列中出现一次的队列, 当que里的合法元素达到 26 个是就是 nice substring 了。
在这过程中 用 map<char, int> ch 维护队列中每个字符出现的次数, 且用 ind 维护队列的首元素的在s中的下标。
得到要求的que以后, 扫一遍map, 把没有出现过的大写字母丢到 队列 ans里去,
然后 输出 s[ 0 ~ ind-1], 输出 nice substring,输出s[ind + 26 ~ sz -1]。
最开始的时候, 脑残的只输出了处理以后的 nice substring。Wrong
answer on pretest 6
之后想了很久才发现 (┬_┬), 其中又有个小注意点, 即非nice substring 部分的 '?' 在输出的时候也要 用字母代替掉, 比如全用'A'。
复杂度 O(n)
Thank you!
------from ProLights
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 26where 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
Note
In 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 asABCDEFGHIJKLMNOPQRSTUVWXYZ 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.
Source
Codeforces Round #372 (Div. 2)
My Solution
two pointers、队列(queue)
用 queue<char> que;维护一个除了 '?'以外所有字符最多在该队列中出现一次的队列, 当que里的合法元素达到 26 个是就是 nice substring 了。
在这过程中 用 map<char, int> ch 维护队列中每个字符出现的次数, 且用 ind 维护队列的首元素的在s中的下标。
得到要求的que以后, 扫一遍map, 把没有出现过的大写字母丢到 队列 ans里去,
然后 输出 s[ 0 ~ ind-1], 输出 nice substring,输出s[ind + 26 ~ sz -1]。
最开始的时候, 脑残的只输出了处理以后的 nice substring。Wrong
answer on pretest 6
之后想了很久才发现 (┬_┬), 其中又有个小注意点, 即非nice substring 部分的 '?' 在输出的时候也要 用字母代替掉, 比如全用'A'。
复杂度 O(n)
#include <iostream> #include <cstdio> #include <string> #include <queue> #include <map> using namespace std; typedef long long LL; const int maxn = 1e6 + 8; map<char, int> ch; queue<char> que, ans; int main() { #ifdef LOCAL freopen("b.txt", "r", stdin); //freopen("o.txt", "w", stdout); int T = 6; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int sz = s.size(); if(sz < 26) cout << -1 << endl; else{ int ptr = 0, ind = 0; while(que.size() < 26){ que.push(s[ptr]); ch[s[ptr]]++; while(s[ptr] != '?' && ch[s[ptr]] > 1){ ch[que.front()]--; que.pop(); ind++; } ptr++; if(ptr >= sz) break; } if(que.size() == 26){ for(int i = 0; i < 26 ; i++){ if(ch[i + 'A'] == 0){ ans.push(i + 'A'); } } for(int i = 0; i < ind; i++){ if(s[i] != '?') cout << s[i]; else cout << 'A'; } while(!que.empty()){ if(que.front() != '?'){ cout << que.front(); } else{ cout << ans.front(); ans.pop(); } que.pop(); } for(int i = ind + 26; i < sz; i++){ if(s[i] != '?') cout << s[i]; else cout << 'A'; } cout << endl; } else{ cout << -1 << endl;} } #ifdef LOCAL ch.clear(); while(!que.empty()) que.pop(); cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- POJ 1141 Brackets Sequence (区间DP)
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
- Android Studio导入项目一直卡在Building gradle project info最快速解决方案
- Sicily 1002. Anti-prime Sequences | 深度优先搜索
- 保存流 出现错误:[FireDAC][Phys]-329. Cannot generate update query. WHERE condition is empty
- android studio 错误的解决方法之一:transformClassesWithDexForDebug' finished with non-zero exit value 3
- 用户界面UI -- 客户端 -- 数据库 交互时时间保护
- Kafka中QueueFullException异常
- VC添加ESXi异常datacenter.queryconnectioninfo
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- Query中ajax的使用与缓存问题的解决方法
- 制作支持UEFI PC的Server2008 R2系统安装U盘
- java基于GUI的定时关机程序