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. day04 列表增删改查、元祖以及range

    01 课前小甜点 千万不要随意做决定 只要你做了决定,你要坚持下去. 02 昨日内容回顾 int <---> bool : 非0 True 0 False True 1 False 0 i ...

  2. MySQL--14 半同步复制

    目录 MySQL半同步复制 半同步复制开启方法 测试半同步 MySQL过滤复制 MySQL半同步复制 从MYSQL5.5开始,支持半自动复制.之前版本的MySQL Replication都是异步(as ...

  3. 第五节:从一条记录说起——InnoDB记录结构

    <MySQL 是怎样运行的:从根儿上理解 MySQL>第五节:从一条记录说起——InnoDB记录结构 准备工作 现在只知道客户端发送请求并等待服务器返回结果.    MySQL什么方式来访 ...

  4. 【LeetCode】一种博弈思路 minimax(共5题)

    [292] Nim Game (2019年3月12日,E) 有一堆石头,游戏规则是每次可以从里面拿1-3颗石头,拿到最后的石头的人赢.你和你的对手都 optimal 的玩这个游戏,问先手(也就是你)能 ...

  5. 请求体中需要的true和requests包put请求冲突了

    python  put请求,添加请求头 不知道怎么解决

  6. 特朗普或出席!富士康耗资100亿美元建LCD液晶面板厂

    富士康建液晶工厂,富士康科技集团发言人证实,富士康科技集团将于今年6月28日耗资100亿美元的LCD面板厂举行动工仪式. 富士康周四表示,他已经了解到,仪式将于今年6月28日举行,包括美国总统特朗普总 ...

  7. thread 类详解

    java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...

  8. Appium API文档中文版

    传送门  https://testerhome.com/topics/3144

  9. leetcode-15双周赛-1286-字母组合迭代器

    题目描述: 方法: class CombinationIterator: def __init__(self, characters: str, combinationLength: int): se ...

  10. 测试md代码折叠功能

    展开查看 System.out.println("Hello to see U!");