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 ...
随机推荐
- lintcode-153-数字组合 II
153-数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素 ...
- 【C/C++语法外功】类的静态成员理解
例1 孙鑫視頻學習 Oct.27th 2009 Skyseraph 例子1.0 #include "iostream" class Point { public: void ...
- Bootstrap排版类
类 描述 实例 .lead 使段落突出显示 尝试一下 .small 设定小文本 (设置为父文本的 85% 大小) 尝试一下 .text-left 设定文本左对齐 尝试一下 .text-center 设 ...
- asp.net mvc4中Json的应用
做一个简单的 Json实例,从页面获取后台的Json数据 1.控制台: public class HomeController : Controller { // // GET: /Home/ pub ...
- Visual Stdio 2015打包安装项目的方法(使用Visual Studio Installer)
首先在官网下载VS2015的Visual Studio Installer 1.创建安装项目 里面最左侧的框框有三个文件夹 1.“应用程序文件夹”即"Application Folder&q ...
- CentOS 服务ftp(vsftpd)
1.检查是否已经安装vsftpd yum list installed | grep vsftpd 2.安装vsftpd yum install -y vsftpd 3.检查vsftpd system ...
- springboot2.0 快速集成kafka
一.kafka搭建 参照<kafka搭建笔记> 二.版本 springboot版本 <parent> <groupId>org.springframework.bo ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- 洛谷 P4735 最大异或和 解题报告
P4735 最大异或和 题目描述 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的 ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...