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. javascript:变量声明&&赋值的提升和函数声明&&定义的提升在不同情况下的表现

    console.log(a); //undefined console.log(show); //函数的定义 show();         //aaa123 var a = 1; function ...

  2. AI-Tensorflow-神经网络优化算法-梯度下降算法-学习率

    记录内容来自<Tensorflow实战Google一书>及MOOC人工智能实践 http://www.icourse163.org/learn/PKU-1002536002?tid=100 ...

  3. rabbitMQ实现推迟队列

    一. 使用原生Api 1.RabbitMQ 相关 <dependency> <groupId>com.rabbitmq</groupId> <artifact ...

  4. 使用transporter同步MongoDB数据到es

    由于logstash不支持MongoDB自定义主键导入es,所以使用transporter导入数据. 版本:es5.x,transporter0.25, es6以上不允许一个索引下面多个type,tr ...

  5. fputc, fputs, putc, putchar, puts - 输出字符和字符串

    总览 (SYNOPSIS) #include <stdio.h> int fputc(int c, FILE *stream); int fputs(const char *s, FILE ...

  6. ed-tue-robotics

    https://github.com/tue-robotics/ed ubuntu16.04 安装libsdformat4-dev ,libsdformat4 1./usr/include/sdfor ...

  7. Linux系统常用知识(centos7)

    一.查看系统版本 1.查看linux内核版本 #cat /etc/redhat-release 二.主机名 2.1定义: 静态的(Static hostname):“静态”主机名也称为内核主机名,是系 ...

  8. 8.xpath(dom4j支持的jar)

    1.使用dom4j支持xpath的操作(xpath可以直接获取到某个元素) (1)第一种形式 /AAA/DDD/BBB:表示一层一层的,AAA下面DDD下面的BBB元素 (2)第二种形式 //BBB: ...

  9. BUUCTF | 摩丝

    将得到的交上去居然不对: 然而大写却过了: flag{ILOVEYOU} 因为摩斯电码在设计的时候就没有区分大小写,而且从码表中可以看到,都是大写,所以在网站上解密出来的自己转成大写

  10. (转)k8s存储之NFS

    转:https://www.cnblogs.com/DaweiJ/articles/9131762.html 1 NFS介绍 NFS是Network File System的简写,即网络文件系统,NF ...