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 ;
}

Codeforce 464A. No to Palindromes!的更多相关文章

  1. codeforce No to Palindromes!(枚举)

    /* 题意:给定一个字符串中没有任何长度>1的回文子串!求按照字典序的该串的下一个字符串 也不包含长度>1的任何回文子串! 思路:从最低位进行枚举,保证第i位 不与 第 i-1位和第 i- ...

  2. 【Codeforces 464A】No to Palindromes!

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为原序列没有任何长度超过2的回文串. 所以,我们在改变的时候,只要时刻保证改变位置s[i]和s[i-1]以及s[i-2]都不相同就好. 因为 ...

  3. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  4. hdu 1318 Palindromes

    Palindromes Time Limit:3000MS     Memory Limit:0KB     64bit                                         ...

  5. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  6. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  7. Dual Palindromes

    Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...

  8. ytu 1940:Palindromes _easy version(水题)

    Palindromes _easy version Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 47  Solved: 27[Submit][Statu ...

  9. 回文串+回溯法 URAL 1635 Mnemonics and Palindromes

    题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...

随机推荐

  1. 【刷题】java 常见的几种运行时异常RuntimeException

    常见的几种罗列如下: -NullPointerException - 空指针引用异常 ClassCastException - 类型强制转换异常. IllegalArgumentException - ...

  2. 20191114PHP图像绘制

    <?phpob_clean();$img=imagecreate(400,400);$back=imagecolorallocate($img,200,200,100); $blue=image ...

  3. JSON对象与JavaScript对象的区别

    //js对象的字面量表示法: var people1={ name:'hehe', age:18 }; //json的格式是: var people1={ "name":'hehe ...

  4. centos 7 命令界面与GUI界面切换

    一.需要重启生效 1. 命令模式     systemctl set-default multi-user.target 2. 图形模式     systemctl set-default graph ...

  5. C# 生成word文档(NPOI.XWPF)

    一.基础 1.创建Word using NPOI.XWPF.UserModel XWPFDocument doc = new XWPFDocument(); //创建新的word文档 XWPFPara ...

  6. I2C走线技巧

  7. Nginx的简单使用

    一.Nginx概述 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.它的特点是支持高并发:资源消耗少:可以做HTTP反向代 ...

  8. 2.WCF学习--地址

    一.URI(统一资源标识) web服务可视为一种网络资源,并且可以通过一个URI来进行唯一标识.而服务通过终结点的形式发布出来,我们所说的一个服务在大部分场景中实际上指的是服务的某个终结点.终结点的核 ...

  9. .NET Core 构建跨平台的桌面应用

    1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...

  10. JS中数据结构之列表

    列表是一组有序的数据.每个列表中的数据项称为元素.在 JavaScript 中,列表中的元素可以是任意数据类型.列表中可以保存多少元素并没有事先限定并可以不断壮大,实际使用时元素的数量受到程序内存的限 ...