C. No to Palindromes!
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.

Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.

Input

The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).

Output

If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).

Sample test(s)
Input
3 3 cba
Output
NO
Input
3 4 cba
Output
cbd
Input
4 4 abcd
Output
abda
Note

String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.

The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.

A palindrome is a string that reads the same forward or reversed.

题解转自:http://www.cnblogs.com/iwtwiioi/p/3961709.html

题意:给你一个没有长度为2以上(包括2)的回文串的一个字符串,然后让你求比他大的所有排列且没有长度为2以上(包括2)的下一个最小的排列。

剩下的1个半小时都在搞这题。。。。。。。。。。T_T

神题。。

我dfs写渣

判重写渣

然后滚粗。

这里有个贪心。

串的任意部分都不能是回文=串中任意连续3个字符互不相同

那么如果从左往右找,那么只需要判断x-1和x-2与x不等即可

其次我们要保证字典序最小,所以我们从右向左依次尝试修改,只要找到不符合回文条件的点,就修改它。这样保证了前边的最小。。

而且能证明,后边一定能构造出一个不是回文的串。例如m=4,我可以构造abcabcabc。。。。。。。。。。。。(x-1和x-2与x不等)

程序中有访问 char 数组的负下标,感觉太高大上了,我试了下,负下标的%d输出为0

网上有人说原因是  :记住,下标只是个操作符。 x[n] 等价于 *(x + n)

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 1005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,p;
char s[N]; char ok(int t)
{
char i;
for(i=s[t]+;i-'a'+<=p;i++){
if(s[t-]!=i && s[t-]!=i) return i;
}
return ;
} int solve()
{
int i;
char t;
// if(p==1) return 0;
// if(n==1) return
for(i=n-;i>=;i--){
t=ok(i);
if( t!= ){
s[i]=t;
break;
}
}
if(i==-) return ; i++;
for(;i<n;i++){
s[i]='a'-;
t=ok(i);
if( t!= ){
s[i]=t;
}
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&p)!=EOF)
{ scanf("%s",s);
// if(p==1){
// printf("NO\n");continue;
// }
if(solve()==){
printf("NO\n");
}
else{
printf("%s\n",s);
}
} return ;
}

Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心的更多相关文章

  1. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

  2. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  3. Codeforces Round #265 (Div. 2) D. Restore Cube 立方体判断

    http://codeforces.com/contest/465/problem/D 给定8个点坐标,对于每个点来说,可以随意交换x,y,z坐标的数值.问说8个点是否可以组成立方体. 暴力枚举即可, ...

  4. Codeforces Round #265 (Div. 2) D. Restore Cube 立方体推断

    http://codeforces.com/contest/465/problem/D 给定8个点坐标.对于每一个点来说,能够任意交换x.y,z坐标的数值. 问说8个点能否够组成立方体. 暴力枚举就可 ...

  5. Codeforces Round #265 (Div. 2)

    http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...

  6. Codeforces Round #265 (Div. 1) C. Substitutes in Number dp

    题目链接: http://codeforces.com/contest/464/problem/C J. Substitutes in Number time limit per test 1 sec ...

  7. Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子

    http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...

  8. Codeforces Round #265 (Div. 2) E. Substitutes in Number

    http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...

  9. Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串

    http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定 ...

随机推荐

  1. ajax的traditional属性

    jquery框架的ajax参数除了常用的 $.ajax({ url: 'xxx', type: 'xxx', data: 'xxx', success: 'xxx' ... }) 外还有一个参数需要特 ...

  2. Cocos2d-X研究之3.0 场景切换特效汇总

    Cocos2d-X研究之3.0 场景切换特效汇总 2014-08-05      0个评论    来源:游戏编程    收藏    我要投稿 cocos2d-x 3.0中场景切换特效比较多,而且游戏开 ...

  3. LeetCode 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

  4. centos7内核优化

    #sysctl -p 参数: net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1net.ipv4.icmp ...

  5. linux-ngnix服务(一)

    httpd MPM: prefork:进程模型,两级结构,主进程master负责生成子进程,每个子进程负责响应一个请求 worker:线程模型,三级结构,主进程master负责生成子进程,每个子进程负 ...

  6. heartbeat+drdb+nfs实现高可用

    一.环境 nfsserver01:192.168.127.101 心跳:192.168.42.101 centos7.3 nfsserver02:192.168.127.102 心跳:192.168. ...

  7. 杭电 5773 The All-purpose Zero

    Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000). ...

  8. 包含min的栈

    #include <iostream> #include <stack> using namespace std; void push(stack<int> &am ...

  9. SQLServer数据库查看死锁、堵塞情况

    在压力测试过程中,不间断的按F5键执行上面的SQL语句,如果出现死锁或者堵塞现象,就会在执行结果中罗列出来.如果每次连续执行SQL,都有死锁或者堵塞出现,说明死锁或者堵塞的比较严重. --每秒死锁数量 ...

  10. Android开发——子线程操作UI的几种方法

    在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 在看方法之前需要了解一下Android中的消息机制. 转载请标明出处:h ...