Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
2024-10-21 12:47:15
题目链接: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();
}
最新文章
- 代码的坏味道(19)——狎昵关系(Inappropriate Intimacy)
- Caliburn.Micro学习笔记目录
- 表单 - Form - EasyUI提供的表单异步提交
- oracle控制文件丢失恢复
- 网络爬虫-使用Python抓取网页数据
- AS3 IOC框架Spring Actionscript 的使用总结
- 关于Could not resolve dependencies for project
- OpenGL结合C#进行绘图
- ajax 和jsonp 不是一码事
- 如何防止自己网站的图片被其他网站所盗用,从而导致自己网站流量的损失【apache篇】
- Codeforces Round #443 (Div. 1) A. Short Program
- Windows 环境下 wampserver 与 phpStudy 的环境配置
- SpringMVC+Thymeleaf +HTML的简单框架
- 【4opencv】CLR基本原理和如何运用于GOCW
- shell日常实战防dos攻击
- Android_设置全屏的方法
- “Hello World!”团队第七周召开的第一次会议
- Qt实现截屏并保存(转载)
- [BZOJ4016][FJOI2014]最短路径树问题(dijkstra+点分治)
- PAT甲级1021. Deepest Root
热门文章
- 如何构建一个多人(.io) Web 游戏,第 1 部分
- zabbix自定义监控nginx
- 修改机器的hostname
- oracle新增ID主键列,如何补全旧数据的ID值
- 一个非常棒的Go-Json解析库
- 目标检测的评价指标(TP、TN、FP、FN、Precision、Recall、IoU、mIoU、AP、mAP)
- 请谨慎使用 avaliable 方法来申请缓冲区
- 2021年官网下载各个版本JDK最全版与官网查阅方法
- The Go Blog Getting to Go: The Journey of Go&#39;s Garbage Collector
- Git提交代码规范 而且规范的Git提交历史,还可以直接生成项目发版的CHANGELOG(semantic-release)