题目链接:Codeforces 464A No to Palindromes!
题目大意:给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,并
且说同样不存在长度大于2的回文子串。
解题思路:直接去构造即可,从最后一位开始,每次只要考虑该字符是否和前两个字符相同即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int N, P;
char s[maxn];
bool check (int v) {
if (v >= 1 && s[v] == s[v-1])
return false;
if (v >= 2 && s[v] == s[v-2])
return false;
return true;
}
bool solve (int v) {
while(true) {
if (v >= N)
return true;
if (v < 0)
return false;
if (s[v] - 'a' == P - 1) {
s[v] = 'a' - 1;
v--;
} else {
int k = (s[v] - 'a' + 1) % P;
s[v] = 'a' + k;
if (check(v))
v++;
}
}
return false;
}
int main () {
scanf("%d%d%s", &N, &P, s);
if (!solve(N-1))
printf("NO\n");
else
printf("%s\n", s);
return 0;
}
分享到:
相关推荐
Codeforces 185A - Plant 全测试点49个
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
潜行者 使用Python进行命令行跟踪
Codeforces global round 10 codes
使用 C# + WPF 开发 ...你只需要提前构造好某些题的叉点数据,填入它,OK!一切就是这么的方便! 注:仅适用于 Edu 以及 Div.3 轮比赛赛后 hack,不支持 Div.1/2 赛时 hack。 适用人群:想进入首页 Hack 榜的选手
Codeforces round 678 division 2 codes
Some of the Codeforces problems codes
lucifer1004大佬的博客cf上分攻略故里大佬的githubcf思维题刷题数:44- (1421)codeforces 676 div2 A,B done
Codeforces round 678 D2_Codeforces_源码
一个Codeforces、牛客竞赛、AtCoder平台的编程竞赛查询插件,ACMer必备.zip
打codeforces的神器
codeforces-js Codeforces JS
codeforces-navi Tampermonkey Codeforces主题扩展 你好! 我为Codeforces制作了一个绿色导航的深色主题,默认情况下,在夜晚这是一场噩梦般的夜晚,解决了问题。 安装: 安装。 安装。 捐款开放:)
Codeforces Round #723 (Div. 2).md
每日CodeForces练习您好,这里通! :brain: :brain: 这是我的代码部队的日常实践。 :exploding_head: :exploding_head: :female_sign: 我准备链接问题和解决方案 :person_raising_hand: :female_sign: 您已克隆并...
codeforces算法比赛题:1295A题