题目链接: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();
}

最新文章

  1. 代码的坏味道(19)——狎昵关系(Inappropriate Intimacy)
  2. Caliburn.Micro学习笔记目录
  3. 表单 - Form - EasyUI提供的表单异步提交
  4. oracle控制文件丢失恢复
  5. 网络爬虫-使用Python抓取网页数据
  6. AS3 IOC框架Spring Actionscript 的使用总结
  7. 关于Could not resolve dependencies for project
  8. OpenGL结合C#进行绘图
  9. ajax 和jsonp 不是一码事
  10. 如何防止自己网站的图片被其他网站所盗用,从而导致自己网站流量的损失【apache篇】
  11. Codeforces Round #443 (Div. 1) A. Short Program
  12. Windows 环境下 wampserver 与 phpStudy 的环境配置
  13. SpringMVC+Thymeleaf +HTML的简单框架
  14. 【4opencv】CLR基本原理和如何运用于GOCW
  15. shell日常实战防dos攻击
  16. Android_设置全屏的方法
  17. “Hello World!”团队第七周召开的第一次会议
  18. Qt实现截屏并保存(转载)
  19. [BZOJ4016][FJOI2014]最短路径树问题(dijkstra+点分治)
  20. PAT甲级1021. Deepest Root

热门文章

  1. 如何构建一个多人(.io) Web 游戏,第 1 部分
  2. zabbix自定义监控nginx
  3. 修改机器的hostname
  4. oracle新增ID主键列,如何补全旧数据的ID值
  5. 一个非常棒的Go-Json解析库
  6. 目标检测的评价指标(TP、TN、FP、FN、Precision、Recall、IoU、mIoU、AP、mAP)
  7. 请谨慎使用 avaliable 方法来申请缓冲区
  8. 2021年官网下载各个版本JDK最全版与官网查阅方法
  9. The Go Blog Getting to Go: The Journey of Go&#39;s Garbage Collector
  10. Git提交代码规范 而且规范的Git提交历史,还可以直接生成项目发版的CHANGELOG(semantic-release)