B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法
2 seconds
256 megabytes
standard input
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?
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的子字符串
代码如下:
#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);
}
}
B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法的更多相关文章
- Codeforces Round #372 (Div. 2) A .Crazy Computer/B. Complete the Word
Codeforces Round #372 (Div. 2) 不知不觉自己怎么变的这么水了,几百年前做A.B的水平,现在依旧停留在A.B水平.甚至B题还不会做.难道是带着一种功利性的态度患得患失?总共 ...
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- Codeforces 716B 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 standar ...
- Codeforces Round #372 (Div. 2) A B C 水 暴力/模拟 构造
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #372 (Div. 2) A ,B ,C 水,水,公式
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph
题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- YaoLingJump开发者日志(七)
LGame用起来真是各种蛋疼,插背景都可以显示不出来.在屏幕结束后释放资源,重载该屏幕时再setbackground也不行,直接用Lpaper当background更不行,会把tilemap上的东 ...
- SQLite - Python
SQLite - Python 安装 SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 2 ...
- VMbox复制虚拟机后网卡问题-bring up interface eth0:Device eth0 does not seem to be present
1.使用 ifconfig -a 查看mac地址 eg:HWaddr:08:00:29:B2:2B 2.vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ...
- Delphi中正常窗口的实现
摘要: 在Delphi的VCL库中,为了使用以及实现的方便,应用对象Application创建了一个用来处理消息响应的隐藏窗口.而正是这个窗口,使得用VCL开发出来的程序存在着与其他窗口不能正常排列平 ...
- Spring Boot 运行原理
Spring Boot并没有任何新的技术,全都是基于Spring4提供的技术,用优秀的设计,为Web开发提供了一套新的方式. 在HelloWorld中,我们没有进行任何显示的配置,但是程序还是运行起来 ...
- request 域 个人理解
1.转发到另一个servlet时候 地址还是输入当前的servlet 2.通过服务器转到另一个servlet时候 另一个servlet是最终接收端 端到端模式 相当于这个东西是发给自己的 只不过经过多 ...
- css的存在形式及优先级
1. 查看源代码---在谷歌浏览器中右击-->点检查 2. CSS中style优先级,标签上的style优先,其它按照编写顺序越更新越优先,后面的会把前面的覆盖掉. 3. 如果想在其它的html ...
- BZOJ5292 & 洛谷4457 & LOJ2513:[BJOI2018]治疗之雨——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5292 https://www.luogu.org/problemnew/show/P4457 ht ...
- BZOJ5338:[TJOI2018]异或——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5338 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操 ...
- css命名冲突解决方法
css的命名冲突目前有几种解决方法: 1.命名约定 人为的制定一下命名规则以避免冲突,例如前缀,嵌套等 2.CSS in JS 在JavaScript中写CSS,使用工具编译为css,最常见的是sty ...