Codeforces Round #265 (Div. 2) C. 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 and
s 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?≤?1000;
1?≤?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.

题意:求满足大于给定串的序列,而且两个序列都不包含长度大于等于2的回文子串

思路:首先,因为不能有长度大于等于2的子串,所以当前位置是不能和它的前一个和前第二个一样的。因为给定串也是不包含回文子串的,所以为了能找个一个最小的满足条件的字符串,我们从右往前改,找到一个满足不与前两个一样的位置的话,那么从这个位置往右就是填写不构成回文的最小串了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1010;

int n, p;
char str[maxn], tmp;

int find(const int &x) {
	if (x == -1)
		return 0;
	for (int i = 1; ; i++) {
		if (str[x] + i >= tmp) break;
		if (x > 0 && str[x-1] == str[x] + i) continue;
		if (x > 0 && str[x-2] == str[x] + i) continue;
		str[x] += i;
		return 1;
	}

	if (!find(x-1))
		return 0;

	str[x] = 'a';
	for (int i = 0; ; i++) {
		if (str[x] + i >= tmp) break;
		if (x > 0 && str[x-1] == str[x] + i) continue;
		if (x > 1 && str[x-2] == str[x] + i) continue;
		str[x] += i;
		return 1;
	}
	return 0;
}

int main() {
	scanf("%d%d", &n, &p);
	tmp = 'a' + p;
	scanf("%s", str);
	if (find(n-1))
		printf("%s\n", str);
	else printf("NO\n");
	return 0;
}
时间: 2024-08-09 06:33:45

Codeforces Round #265 (Div. 2) C. No to Palindromes!的相关文章

Codeforces Round #265 (Div. 2)

Codeforces Round #265 (Div. 2) 题目链接 A:把数字变换后比较一下几个不一样即可 B:连续2个以上0当作一次操作,开头的0和结尾的0可以忽略 C:贪心从末尾去构造,由于保证一开始是回文,所以保证修改后出现回文只可能为长度2或3的,这样的话判断复杂度就很小了 D:暴力枚举情况,然后判断 E:把操作逆过来处理出每个数字对应的位数和相应数字,然后在for一遍计算答案即可 代码: A: #include <cstdio> #include <cstring>

Codeforces Round #265 (Div. 2) 465A. inc ARG(数学题)

题目链接:http://codeforces.com/problemset/problem/465/A Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of n bits. These bits are numbered from 1 to n. An integer is stored in the cell in t

Codeforces Round #265 (Div. 1)

A No to Palindromes! 题意:给一个长度为n的用前m个字符构成的字符串,定义一个字符串是好的当且仅当他的每个子串都不是回文的,现在给出一个好的字符串,求比他字典序大的第一个好的字符串. 题解:从后往前每一位枚举,若把当前枚举的位改成ch后为好的串,只需要判断他和他前面的一个字符是否相同构成长度为2的回文串,或者和他前面的前面的两个字符构成长度为3的回文串. 于是找到第一个可以换的位置,后面每个位置从'a'开始枚举可以取得的第一个字符即可. 1 #include <cstdio>

Codeforces Round #265 (Div. 2) 题解

A:给你一个二进制数,问你加一以后改变多少位 解题思路:乱搞 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月07日 星期日 23时27分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #includ

Codeforces Round #265 (Div. 2) B. Inbox (100500)

Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread. Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the cont

Codeforces Round #265 (Div. 2) D. Restore Cube

Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece o

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe

Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?

这道题竟然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,其中A=p/q: 然后要你找到对于给定的A,找到使得π(n)?≤?A·rub(n) 最大的n. (A<=42) 思路: 首先我们可以暴力算出当n为大概150万左右的时候,π(n)大概是 rub(n) 的42倍. 所以我们只需要for到150万左右就好,因为对于后面的式子,肯定能在150万的范围内找到一个n使得这个式子成立的. 而且,我们可以得出因为素

Codeforces Round #315 (Div. 2) 568A Primes or Palindromes?

题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以可以先暴力求下最大的n满足上式,可以想象下随着n的增大A也在增大(总体正相关,并不是严格递增的),所以二分查找时不行的,所以对给定的A,n是一定存在的.这个题的关键就是快速得到素数表最好在O(n)的时间以内.(杭电15多校的一个题也用到了这个算法点这里查看) 1 #include <bits/std