首页 技术 正文
技术 2022年11月14日
0 收藏 416 点赞 4,565 浏览 1901 个字

A. 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 sdoesn’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 nsmall 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 = tisi + 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.

题目要求了 一个串任何子串都是非回文串,很容易想得到只要 一个串第 i 个位置与 i – 1 与 i – 2 都不相同的话 ,就符合条件了 。

然后题目要求 构造一个字典序大于给出的串的合法串,并且字典序尽量少 。

当时做的时候老想着从后向前扫,得到的必定最少,这样的想法有问题,因为后面的字符更改了再改前面的字符这样是存在后效性的 。

正确的做法是从前向后面开始扫 ,然后就是记录一下前面是否对串进行过更改 , 这样就可以得到正解了 ~

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
int n , m;
char s[N],ans[N];
int tag = ;bool solve( int pos , int flag )
{
int i ;
if(pos == n) {
return ;
}
if( flag )
i = ;
else {
i = s[pos] ;
if( pos == n - ) i++;
} for( ; i < m ; ++i ){
if( ( pos > && i == ans[ pos- ] ) || ( pos > && i == ans[pos - ]) )continue;
ans[pos] = i ;
if( i > s[pos] )flag = ;
if( solve ( pos+ , flag) )return ;
}
return ;
}int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif while( cin>>n>>m ){
scanf("%s",s);
for(int i = ; i < n ; ++i ){
s[i] -= 'a';
}
if( !solve( , ) ){
cout<<"NO";
}
else {
for(int i = ;i < n ; ++i)
cout<< (char) (ans[i] + 'a');
}
cout<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297