Codeforces Round #265 (Div. 2)-C. No to Palindromes!
2016-10-25 15:36
555 查看
原题链接
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 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).
Examples
input
output
input
output
input
output
对于每个字符若str[i] != str[i-1] && str[i] != str[i-2]则必定不会出存在回文串.从尾到头遍历字符串str,对于str[i], 若存在字符变量p使得p > str[i] && p != str[i-1] && p != str[i-2]则str[i] = p, 从j = i + 1开始找到最小的字符,为str[j]赋值满足不为回文串的条件
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 1005
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long ll;
char str[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, p;
scanf("%d%d", &n, &p);
scanf("%s", str);
int sign = -1;
for(int i = n-1; i >= 0; i--){
for(int j = str[i] + 1; j < p + 'a'; j++){
if(i && j == str[i-1])
continue;
if(i > 1 && j == str[i-2])
continue;
str[i] = j;
sign = i;
break;
}
if(sign != -1)
break;
}
if(sign == -1){
puts("NO");
return 0;
}
for(int i = sign+1; i < n; i++){
for(int j = 'a'; j < p + 'a'; j++){
if(i && j == str[i-1])
continue;
if(i > 1 && j == str[i-2])
continue;
str[i] = j;
break;
}
}
puts(str);
return 0;
}
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 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).
Examples
input
3 3 cba
output
NO
input
3 4 cba
output
cbd
input
4 4 abcd
output
abda
对于每个字符若str[i] != str[i-1] && str[i] != str[i-2]则必定不会出存在回文串.从尾到头遍历字符串str,对于str[i], 若存在字符变量p使得p > str[i] && p != str[i-1] && p != str[i-2]则str[i] = p, 从j = i + 1开始找到最小的字符,为str[j]赋值满足不为回文串的条件
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 1005
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long ll;
char str[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, p;
scanf("%d%d", &n, &p);
scanf("%s", str);
int sign = -1;
for(int i = n-1; i >= 0; i--){
for(int j = str[i] + 1; j < p + 'a'; j++){
if(i && j == str[i-1])
continue;
if(i > 1 && j == str[i-2])
continue;
str[i] = j;
sign = i;
break;
}
if(sign != -1)
break;
}
if(sign == -1){
puts("NO");
return 0;
}
for(int i = sign+1; i < n; i++){
for(int j = 'a'; j < p + 'a'; j++){
if(i && j == str[i-1])
continue;
if(i > 1 && j == str[i-2])
continue;
str[i] = j;
break;
}
}
puts(str);
return 0;
}
相关文章推荐
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
- Codeforces Round #265 (Div. 2) C. No to Palindromes!
- Codeforces Round #265 (Div. 2) C. No to Palindromes!(字符串+构造??)
- Codeforces Round #265 (Div. 1) A No to Palindromes!
- Codeforces Round #265 (Div. 2) C - No to Palindromes!
- Codeforces Round #265 (Div. 2) C.No to Palindromes!
- Codeforces Round #265 (Div. 2) C. No to Palindromes!
- Codeforces Round #265 (Div. 2)
- Codeforces Round #265 (Div. 2)D. Restore Cube 暴力
- Codeforces Round #265 (Div. 1) C. Substitutes in Number dp
- Codeforces Round #265 (Div. 2) D. Restore Cube
- Codeforces Round #239 (Div. 2) A. Line to Cashier(简单题)
- Codeforces Round #360 (Div. 2) B Lovely Palindromes(水题)
- Codeforces Round #265 (Div. 2) B. Inbox (100500)(模拟)
- Codeforces Round #265 (Div. 1) B Restore Cube
- Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心
- Codeforces Round #265 (Div. 2) A. inc ARG
- Codeforces Round #265 (Div. 2) B. Inbox (100500)