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. leetcode_1095. Find in Mountain Array_[Binary Search]

    https://leetcode.com/problems/find-in-mountain-array/ 题意:给定一个MountainArray(定义见题目),找到其中最早出现的target值的下 ...

  2. 上下文 xx

    上下文,就是指在程序中的某个位置,可以访问到的所有资源的总和. 具体说来,在程序中资源可能是一个变量.一个常量.一个类的引用等等.

  3. 中国剩余定理&Lucas定理&按位与——hdu 5446

    链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...

  4. 单源最短路SPFA

    #include<iostream> #include<queue> #include<cstring> #define INF 0x3f3f3f3f using ...

  5. 获取显示设备的名称及PNPDeviceID

    实现效果: 知识运用: ManagementObjectSearcher类和ManagementObject类 实现代码: private void button1_Click(object send ...

  6. #include <> 和 #inlude ""的区别

    #include < >引用的是编译器的类库路径里面的头文件#include  " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...

  7. [BZOJ2938]病毒 (AC自动机+dfs)

    题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...

  8. IE(IE6/IE7/IE8)支持HTML5标签--20150216

    让IE(ie6/ie7/ie8)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个j ...

  9. C++系统学习之四:数组

    与vector的异同 相同:都是存放类型相同对象的容器 不同:数组的大小确定不变,不能随意向数组中增加元素 1.定义和初始化内置数组 数组中元素的个数也属于数组类型的一部分,编译的时候维度应该是已知的 ...

  10. CentOS 7 编译 mysql 8.0.12

    步骤一:安装mysql依赖 yum install -y libaio numactl 步骤二:下载mysql社区版 wget https://dev.mysql.com/get/Downloads/ ...