Codeforces Round #265 (Div. 2) C.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 ≤ 10001 ≤ 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.


第一次做出来C题, 但还觉得有所思, , 应该归于 模拟类的吧 ;

题解 : 对于每一个字母, 只需判断他与之前两个字母是否合法就行 , 

比赛后, 调试简化的代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>`
#include <algorithm>
#include <cctype>
#include <queue>
#include <vector>
#define INF 100000000     //0x7fffffff
#define eps (1e-9)
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
int n,m,p;
char st[1100],ans[1100];
int check(int id){
    st[id]++; int c =st[id]-96;
    while(c<=p){
        if(st[id]!=st[id-1] && st[id]!=st[id-2])
        {  return 1;  }        st[id]++;  c++ ;
    }
    return 0;
}
int main()
{
    //freopen("D:\data.txt","r",stdin);
    int TT,t =0,i,k;  int ok =0,flag=1;
    scanf("%d %d",&n,&p); getchar();  scanf("%s",st);   //printf("%s\n",st);
    //------------------
    if(n==1){
        if(st[0]-96<p) printf("%c",st[0]+1);
        else           printf("NO");             return 0;
    }
    if(n>2&&check(n-1))     {  printf("%s",st);  return 0;     }
    //------------------
    int id =n-2;
    while(id>=2){
       if(check(id)==0){  id--;  continue;  }    //printf("%d %c %s\n",id,st[id],st);
       for(i=id+1;i<n;i++)
       {   st[i]=96;             check(i);  }
       ok =1;         id=0;//或 break;
    }
    if(ok) { printf("%s",st);    return 0; }
    //------------------
    flag=0;
    if(st[1]-96<p)
    {
        st[1]++; flag=1;
        if(st[1]==st[0]){
        flag=0;
        if(st[1]-96<p) {      st[1]++;     flag=1;   }
        else if(st[0]-96<p) { st[1]=st[0]; st[0]++;  flag=1; }
        }
    }
    else if(st[1]-96==p&&st[0]-96<p)
    {       st[0]++;     st[1]=97;            flag=1;        }
    if(flag==0){      printf("NO");           return 0;      }
    for(i=2;i<n;i++)
    {   st[i]=96;     check(i);      }
    if(flag){         printf("%s",st);        return 0;      }
    //-------------------
    printf("NO");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值