题意:给你一个字符串,寻找按照字典序排的下一个符合条件的字符串
下一个满足的字符串一定是改变尽量靠右的字符才能得到(字典序),
从最右面开始找,假设当前是第i位,如果i位增加之后满足和i-1,i-2位均不相同的话,
那么最小的肯定就是从这里开始改的(因为前缀没有回文字串,所以改变i位后的串也不会有),
接下来就把后面的为都补全。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1005;
char c[N];
int main()
{
int n, p;
while(~scanf("%d%d", &n, &p))
{
scanf("%s", c);
int pos = n - 1;
c[pos]++;
while( pos >= 0 && pos < n )
{
if( c[pos] >= 'a' + p)
{
c[pos] = 'a';
c[--pos] ++;
}
else if( c[pos] == c[pos-1] && pos >= 0 || c[pos] == c[pos - 2] && pos >= 1 )
{
c[pos]++;
}
else
{
pos++;
}
}
if(pos > 0)
printf("%s\n", c);
else
printf("NO\n");
}
return 0;
}