Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A
题意
给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母,问能否字符串中所有相邻字母都不同。
题解
除非一开始字符串就不合法,否则一定可以构造出合法的字符串,因为共有三个字母可选,而替换时最多需要判断前后两个位置。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
string s; cin >> s;
for (int i = 1; i < s.size(); i++)
if (islower(s[i]) and s[i] == s[i - 1]) {
cout << -1 << "\n";
return;
}
for (int i = 0; i < s.size(); i++)
if (s[i] == '?')
for (char c : {'a', 'b', 'c'})
if (i == 0) {
if (c != s[i + 1])
s[i] = c;
} else if (i == s.size() - 1) {
if (c != s[i - 1])
s[i] = c;
} else {
if (c != s[i - 1] and c != s[i + 1])
s[i] = c;
}
cout << s << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)的更多相关文章
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors
链接: https://codeforces.com/contest/1265/problem/E 题意: Creatnx has n mirrors, numbered from 1 to n. E ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest
链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has c ...
- Codeforces Round #604 (Div. 2) B. Beautiful Numbers
链接: https://codeforces.com/contest/1265/problem/B 题意: You are given a permutation p=[p1,p2,-,pn] of ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...
- Codeforces Round #604 (Div. 2) B. Beautiful Numbers(双指针)
题目链接:https://codeforces.com/contest/1265/problem/B 题意 给出大小为 $n$ 的一个排列,问对于每个 $i(1 \le i \le n)$,原排列中是 ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)
题目链接:https://codeforces.com/contest/1265/problem/C 题意 从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌 ...
- Codeforces Round #604 (Div. 1) - 1C - Beautiful Mirrors with queries
题意 给出排成一列的 \(n\) 个格子,你要从 \(1\) 号格子走到 \(n\) 号格子之后(相当于 \(n+1\) 号格子),一旦你走到 \(i+1\) 号格子,游戏结束. 当你在 \(i\) ...
随机推荐
- Ts有限状态机
ts版本的有限状态机 最近做小游戏要做切换人物状态,花点时间写了一个有限状态机,使用语言为Ts,也可改成自己的语言 按照目前的逻辑,这个可以继续横向扩展,某些做流程管理 先上预览图 Fsm:状态机类 ...
- Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务
Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...
- mysql的导入
方法1 load data [local] infile 'filename' into table tablename[option] ields terminated by 'string'(字段 ...
- 【Linux】CentOS7中修改中文字符集
CentOS 7中字符集查看的方式是 locale -a 或者locale 如果想显示中文的话,应该修改为 LANG="zh_CN.UTF-8" 在命令行界面临时修改字符集的话 ...
- 3A限流IC,带短路保护,PW1503和PW1502
一般说明 PW1503,PW1502是超低RDS(ON)开关,具有可编程的电流限制,以保护电源源于过电流和短路保护.它具有超温保护以及反向闭锁功能. PW1503,PW1502采用薄型(1毫米)5针薄 ...
- 1.5V升压3V集成电路升压芯片
干电池1.5V升压3V的升压芯片,适用于干电池升压产品输出3V供电 1.5V输入时,输出3V,电流可达500MA. PW5100是一款效率大.10uA低功耗 PW5100输入电压:0.7V-5V PW ...
- SQL Server 2012 忘记sa用户处理方法
SQL Server 2012 忘记sa用户的密码,可重置sa密码,方法如下: 1.将身份验证改成Windows身份验证,登录进去 2.进入SQL Server控制台,在对象资源管理器中找到Secur ...
- pycharm安装完成后的一些基本设置
1.设置背景色 file-->Setting-->Appearance&Behavior-->Appearance 2.设置主题 settings --> editor ...
- 关于jmeter客户端实现中HttpClient4与Java的区别
如上图:jmeter客户端实现方式有三种,一种是java,一种是httpclient4,还有一种默认,我们来看一下java与httpclient4的区别: Java:选择压测时,链接是复用的(代码中的 ...
- Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf/
Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf ...