codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A
题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 n 额下一个tolerable 的 字符串,如果求不出则输出“NO”。字符串被认定为tolerable 需要满足:1、字符串的每一位都是取自字母表中前 p 个字母 2、不是回文串。
直接模拟就可以了。主要包括两个步骤:
(1)从后往前遍历,找到目标位置,也就是需要改动的位置下标。
对于当前需要判断的字符(假设数组下标为i),检查 s[i],s[i-1], s[i-2] 的字符是否相同,不等的话代表找出了目标位置。当然,如果 n = 2,只需要判断s[i] 和 s[i-1]
(2)分析这个下标对下的一个位置到结束位置,每个字符需要作怎样的变动。
有可能目标位置变动后,导致下一个字符和它相等,此时设为 a。
处理的时候,将小写字母转化为整数来处理。
以后尽量拿英语来写注释,希望能锻炼一下蹩脚的英语,可能会有点语法问题,大家请见谅。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
char s[maxn];
int tmp[maxn]; int main()
{
int n, p;
// freopen("in.txt", "r", stdin);
while (scanf("%d%d", &n, &p) != EOF)
{
scanf("%s", s);
for (int i = ; i < n; i++)
tmp[i] = s[i] - 'a' + ;
int k;
int flag = ;
for (int i = n-; i >= && !flag; i--) // find the changed object's id
{
for (int j = tmp[i]+; j <= p && !flag; j++)
{
if (i == ) // n(length) == 1
{
tmp[k=i] = j;
flag = ;
}
else if (i == && j != tmp[i-])
{
tmp[k=i] = j;
flag = ;
}
else if (i >= && j != tmp[i-] && j != tmp[i-])
{
tmp[k=i] = j;
flag = ;
}
}
}
if (!flag)
printf("NO\n");
else
{
k++;
for (; k < n; k++)
{
flag = ;
for (int j = ; j <= p && !flag; j++)
{
if (k == && j != tmp[k-])
{
tmp[k] = j;
flag = ; // ensure every character change only once && the final result is the least lexicographical order
}
else if (k >= && j != tmp[k-] && j != tmp[k-])
{
tmp[k] = j;
flag = ;
}
}
}
for (int i = ; i < n; i++)
printf("%c", tmp[i]-+'a');
puts("");
}
}
return ;
}
codeforces 465C.No to Palindromes! 解题报告的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- PyTorch学习笔记之nn的简单实例
method 1 import torch from torch.autograd import Variable N, D_in, H, D_out = 64, 1000, 100, 10 x = ...
- 简单了解HTML5中的Web Notification桌面通知
原文:http://www.zhangxinxu.com/wordpress/2016/07/know-html5-web-notification/ 需要注意的是,消息通知只有通过Web服务访问该页 ...
- iOS Framework: Introducing MKNetworkKit (MKNetworkKit介绍,入门,翻译)
这片文章也有塞尔维亚-克罗地亚语(由Jovana Milutinovich翻译)和日语(由@noradaiko翻译) 如果有个一个网络库能够自动的为你处理cache该有多好啊. 如果有一个网络库能够在 ...
- xamarin android 获取根证书代码
Java.Security.KeyStore keyStore = Java.Security.KeyStore.GetInstance("AndroidCAStore"); ke ...
- clipRect 介绍
clipRect 介绍 博客分类: android android的clip有以下两点疑问: Clip(剪切)的时机 Clip中的Op的参数的意思. 通常咱们理解的clip(剪切),是对已经存 ...
- JavaScript中的Math方法演示
<html> <head> <script type="text/javascript"> var num = 12.4; alert(Math ...
- HRBUST2030(dfs)
成语接龙 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class nam ...
- python(13)- 文件处理应用Ⅱ:增删改查
用户选择1,增加功能: 用户输入www.oldboy2.org和server 11111 weight 2222 maxconn 3333后, 在www.oldboy2.org下增加一条server信 ...
- WPF 基础到企业应用系列1——开篇故意
參考资料 提到參考资料,大家第一感觉就是MSDN,当然我也不例外.这个站点基本上是学习微软技术的首选站点,除了这个站点以外,我还參考了非常多其它的社区和站点,基本上都在.NET 技术社区之我见(英文篇 ...
- C语言关键字—-sizeof 、typedef、const、static、register、extern、#define
关键字:sizeof .#define.typedef.const.static.register.extern sizeof 1. 作用:求数据所占得内存空间大小 2. 本质:求数据得类型所占的内存 ...