Codeforces 464A No to Palindromes!

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 ands 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 ≤ 10001 ≤ 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.


解题思路:首先我们应该明白对于第i个字符串的添加,我们只要考虑和第i-1,i-2位置处的字符不能构成回文串即可。s[i-1]s[i]和s[i-2],s[i-1],s[i]以上两种情况不能构成回文的话,则所有子串肯定不是回文串。因此我们可以采用如下的方法,首先我们要找出可以修改的最后面的位置,从该位置向后我们只需要从小到大枚举然后满足我上述提到的条件即可,这样构造出来的字符串肯定是当前串的下一个字符串。

#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 1010;
char str[maxn];

int main() {

    int n, p, i, j;
    scanf("%d %d", &n, &p);
    scanf("%s", str);
    for(i = n - 1; i >= 0; --i) {
        char s1 = ' ';
        char s2 = ' ';
        if(i - 1 >= 0) {
            s1 = str[i-1];
        }
        if(i-2 >= 0) {
            s2 = str[i-2];
        }
        for(j = str[i]+1; j < 'a'+p; ++j) {
            if(j == s1 || j == s2) continue;
            str[i] = j;
            break;
        }
        if(j < 'a'+p) break;
    }
    if(i >= 0) {
        for(i = i + 1; i < n; ++i) {
            char s1 = ' ';
            char s2 = ' ';
            if(i - 1 >= 0) {
                s1 = str[i-1];
            }
            if(i - 2 >= 0) {
                s2 = str[i-2];
            }
            for(j = 'a'; j < 'a'+p; ++j) {
                if(s1 == j || s2 == j) continue;
                str[i] = j;
                break;
            }
        }
    }
    if(i < 0) {
        printf("NO\n");
    } else {
        printf("%s\n", str);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值