Codeforces Round #604 (Div. 2) 部分题解
链接:http://codeforces.com/contest/1265
A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.
Ahcl wants to construct a beautiful string. He has a string ss, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters '?', the condition si≠si+1si≠si+1 should be satisfied for all 1≤i≤|s|−11≤i≤|s|−1, where |s||s| is the length of the string ss.
The first line contains positive integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt lines contain the descriptions of test cases.
Each line contains a non-empty string ss consisting of only characters 'a', 'b', 'c' and '?'.
It is guaranteed that in each test case a string ss has at least one character '?'. The sum of lengths of strings ss in all test cases does not exceed 105105.
For each test case given in the input print the answer in the following format:
- If it is impossible to create a beautiful string, print "-1" (without quotes);
- Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.
3
a???cb
a??bbc
a?b?c
ababcb
-1
acbac
In the first test case, all possible correct answers are "ababcb", "abcacb", "abcbcb", "acabcb" and "acbacb". The two answers "abcbab" and "abaabc" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 44-th and 55-th characters will be always equal.
In the third test case, the only answer is "acbac".
题意很简单,给定一个只含a,b,c,?的字符串,如果字符串出现连续相同的字母(!='?'),就是输出-1,否则把?处填上字母,保证不会出现连续相同字母
自己写的实在是太麻烦,把几种情况都考虑进去了: 1:???? 2:a????? 3:??????a 对比其他人的代码,感觉自己真是蠢,还是菜!
先上自己的渣渣代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5+;
char s[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>s;
int len = strlen(s);
int ok = ,ok2=;
for(int i = ; i < len; i++)
{
if(s[i]==s[i+]&&s[i]!='?')
ok=;
if(s[i]!='?')
ok2=;
}
if(!ok2)
{
s[]='a';
for(int i = ; i < len ; i++)
{
if(s[i-]=='c')
s[i]='a';
else
s[i]=s[i-]+;
}
cout<<s<<endl;
continue ;
}
if(ok)
{
cout<<"-1"<<endl;
continue;
}
for(int i = ; i< len;i++)
{
if(s[i]=='?')
{
int l=i;
int r;
for(int j = i;j<len;j++)
{
if(s[j]!='?')
break;
r=j;
// cout<<r<<endl;
}
//cout<<r<<" "<<len<<endl;
if(i==)
{
for(int j=r;j>=l;j--)
{
if(s[j+]=='a')
s[j]='c';
else
s[j]=s[j+]-;
}
}
else if(r==len-)
{ for(int j=l;j<=r;j++)
{
if(s[j-]=='c')
s[j]='a';
else
s[j]=s[j-]+;
}
}
else
{
for(int j=l;j<r;j++)
{
if(s[j-]=='c')
s[j]='a';
else
s[j]=s[j-]+;
}
if(s[r-]=='a'&&s[r+]=='a'||s[r-]=='a'&&s[r+]=='b'||s[r-]=='b'&&s[r+]=='a')
s[r]='c';
else if(s[r-]=='b'&&s[r+]=='b'||s[r-]=='b'&&s[r+]=='c'||s[r-]=='c'&&s[r+]=='b'||s[r-]=='c'&&s[r+]=='c')
s[r]='a';
else
s[r]='b';
}
}
}
cout<<s<<endl;
}
}
赛后参考别人然后自己写的代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5+;
char s[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>s;
int len=strlen(s);
int ok = ;
for(int i = ; i< len ; i++)
{
if(s[i]==s[i+]&&s[i]!='?')
{
ok=;break;
}
if(s[i]=='?')
{
for(int j=;j<;j++)
{
if('a'+j!=s[i-]&&'a'+j!=s[i+]) //管他?在什么位置,直接中间不能于两边就上就行了。
{
s[i]='a'+j;break;
} }
}
}
if(ok)
cout<<"-1"<<endl;
else
cout<<s<<endl;
}
}
B-Beautiful Numbers
You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn] of integers from 11 to nn. Let's call the number mm (1≤m≤n1≤m≤n) beautiful, if there exists two indices l,rl,r (1≤l≤r≤n1≤l≤r≤n), such that the numbers [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m.
For example, let p=[4,5,1,3,2,6]p=[4,5,1,3,2,6]. In this case, the numbers 1,3,5,61,3,5,6 are beautiful and 2,42,4 are not. It is because:
- if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1;
- if l=3l=3 and r=5r=5 we will have a permutation [1,3,2][1,3,2] for m=3m=3;
- if l=1l=1 and r=5r=5 we will have a permutation [4,5,1,3,2][4,5,1,3,2] for m=5m=5;
- if l=1l=1 and r=6r=6 we will have a permutation [4,5,1,3,2,6][4,5,1,3,2,6] for m=6m=6;
- it is impossible to take some ll and rr, such that [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m for m=2m=2 and for m=4m=4.
You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn]. For all mm (1≤m≤n1≤m≤n) determine if it is a beautiful number or not.
The first line contains the only integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. The next lines contain the description of test cases.
The first line of a test case contains a number nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the given permutation pp. The next line contains nnintegers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are different) — the given permutation pp.
It is guaranteed, that the sum of nn from all test cases in the input doesn't exceed 2⋅1052⋅105.
Print tt lines — the answers to test cases in the order they are given in the input.
The answer to a test case is the string of length nn, there the ii-th character is equal to 11 if ii is a beautiful number and is equal to 00 if ii is not a beautiful number.
3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2
101011
11111
1001
The first test case is described in the problem statement.
In the second test case all numbers from 11 to 55 are beautiful:
- if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1;
- if l=3l=3 and r=4r=4 we will have a permutation [1,2][1,2] for m=2m=2;
- if l=2l=2 and r=4r=4 we will have a permutation [3,1,2][3,1,2] for m=3m=3;
- if l=2l=2 and r=5r=5 we will have a permutation [3,1,2,4][3,1,2,4] for m=4m=4;
- if l=1l=1 and r=5r=5 we will have a permutation [5,3,1,2,4][5,3,1,2,4] for m=5m=5.
题意:给出n, n个数, 1~n。问这个排列是不是有x,区间长度为x,之内是1~x的全排列。呃是不是不太明了,看样例。
比如样例3: 1 4 3 2
idx: 1 2 3 4
l=1,r=1, n= 1,可以。
对于2:要想有1,2 idx处是1~4,很明显多了其他的数,不是1~2的全排列。
对于3: 要想有1,2,3,idx处是1~4,4个元素,肯定不行。
对于4:1~4,可以了。
对于这个题,暴力会超时的,所以我们从l,r入手,不断更新l,r,即代码中的,minn,maxx。一定让区间包括1~x,如果需要阔R,就更新maxx,阔L,更新minn:
maxx=max(maxx,idx[i]);
minn=min(minn,idx[i]); //idx记录坐标
如果maxx-minn+1(表示区间内数字的数目)比需要全排列的x(x的全排列就是x个)还大,那肯定不行,所以需要maxx-minn+1==x
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5+;
const int inf = 1e9;
int idx[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int x;
for(int i = ;i<=n;i++ )
{
cin>>x;
idx[x]=i;
}
int maxx=-;
int minn = inf;
for(int i=;i<=n;i++)
{
maxx=max(maxx,idx[i]);
minn=min(minn,idx[i]);
if((maxx-minn+)==i)
cout<<"";
else
cout<<"";
}
cout<<endl;
}
}
Codeforces Round #604 (Div. 2) 部分题解的更多相关文章
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #545 (Div. 1) 简要题解
这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
随机推荐
- 设备树DTS 学习:2-设备树语法
背景 通过上一讲了解完设备树DTS有关概念,我们这一讲就来基于设备树例程,学习设备树的语法规则. 参考:设备树详解dts.设备树语法详解.设备树使用总结 设备树框架 1个dts文件 + n个dtsi文 ...
- 第二单元总结:基于synchronize锁的简单多线程设计
单元统一的多线程设计策略 类的设计 电梯 每部电梯为一个线程. 电梯从调度器接收原子指令,知晓自己的状态(内部的人/服务的人.运行方向.所在楼层) 原子指令包括且仅包括: 向上走一层 / 向下走一层 ...
- 洛谷 P3435 [POI2006]OKR-Periods of Words
题目传送门 解题思路: 这道题题面比较乱,先说一下这道题要求什么: 对于一个字符串,求它及它的所有前缀的一个答案串的长度之和,答案串就是对于一个字符串,找到一个它的一个前缀,这个前缀后面在复制一遍,得 ...
- 清北学堂例题 LUOGU2523【HAOI2011】problem c
题目描述 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1 ...
- [U53204] 树上背包的优化
题目链接 本文旨在介绍树上背包的优化. 可见例题,例题中N,M∈[1,100000]N,M \in [1,100000]N,M∈[1,100000]的数据量让O(nm2)O(nm^2)O(nm2)的朴 ...
- Ubuntu18.04 LTS 搭建Cassandra集群
环境需求 jdk8 root@node01:~# java -version java version "1.8.0_202" Java(TM) SE Runtime Enviro ...
- vue-i18n多语言文件归类的两种方法
1.按语言类型归类 流行的做法是按照语言对文件进行归类,目录结构类似于: --lang ----en ------test.json --------"abc": "ab ...
- POJ 2823 滑动窗口 单调队列模板
我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求: f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0 ...
- Golang的选择结构-if语句
Golang的选择结构-if语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.选择语句应用场景概述 选择结构也称为条件判断,生活中关于判断的场景也非常的多,比如: ()登录Q ...
- POJ 3077 : Rounders
Rounders Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7827 Accepted: 5062 Description ...