Codeforce 464A. No to Palindromes!
1 second
256 megabytes
standard input
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.
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 nsmall English letters. It is guaranteed that the string is tolerable (according to the above definition).
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
3 3
cba
NO
3 4
cba
cbd
4 4
abcd
abda
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 都不相同的话 ,就符合条件了 。
然后题目要求 构造一个字典序大于给出的串的合法串,并且字典序尽量少 。
当时做的时候老想着从后向前扫,得到的必定最少,这样的想法有问题,因为后面的字符更改了再改前面的字符这样是存在后效性的 。
正确的做法是从前向后面开始扫 ,然后就是记录一下前面是否对串进行过更改 , 这样就可以得到正解了 ~
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
int n , m;
char s[N],ans[N];
int tag = ; bool solve( int pos , int flag )
{
int i ;
if(pos == n) {
return ;
}
if( flag )
i = ;
else {
i = s[pos] ;
if( pos == n - ) i++;
} for( ; i < m ; ++i ){
if( ( pos > && i == ans[ pos- ] ) || ( pos > && i == ans[pos - ]) )continue;
ans[pos] = i ;
if( i > s[pos] )flag = ;
if( solve ( pos+ , flag) )return ;
}
return ;
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif while( cin>>n>>m ){
scanf("%s",s);
for(int i = ; i < n ; ++i ){
s[i] -= 'a';
}
if( !solve( , ) ){
cout<<"NO";
}
else {
for(int i = ;i < n ; ++i)
cout<< (char) (ans[i] + 'a');
}
cout<<endl;
}
return ;
}
Codeforce 464A. No to Palindromes!的更多相关文章
- codeforce No to Palindromes!(枚举)
/* 题意:给定一个字符串中没有任何长度>1的回文子串!求按照字典序的该串的下一个字符串 也不包含长度>1的任何回文子串! 思路:从最低位进行枚举,保证第i位 不与 第 i-1位和第 i- ...
- 【Codeforces 464A】No to Palindromes!
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为原序列没有任何长度超过2的回文串. 所以,我们在改变的时候,只要时刻保证改变位置s[i]和s[i-1]以及s[i-2]都不相同就好. 因为 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Dual Palindromes
Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...
- ytu 1940:Palindromes _easy version(水题)
Palindromes _easy version Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 47 Solved: 27[Submit][Statu ...
- 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...
随机推荐
- resolver - 解析器(resolver) 配置文件
总览 (SYNOPSIS) /etc/resolv.conf 描述 (DESCRIPTION) 解析器(resolver) 是 C 函数库 中 的 一组 例程, 用于 访问 Internet 域名系统 ...
- rk3288 android5.1 修改时区
/work/rk3288/firefly-rk3288_android5.1_git_20180126/device/rockchip/rk3288/rk3288_box/system.prop 修改 ...
- Codeforces Round #420 (Div. 2) - B
题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...
- (ACM模板)集合set
#include<iostream> #include<cstdio> #include<set> using namespace std; int main() ...
- django学习笔记--数据库中的多表操作
1.Django数据库----多表的新增操作 1.一对一模式下新增 创建一个详情对象,把这个对象赋值给创建的新的user对象 author_detail = models.AuthorDetail.o ...
- 九、Rxjs请求对Observable进行封装
1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...
- Hibernate 一对一(基于唯一外键的关联)
主表 hbm.xml中 使用<one-to-one> 从表hbm.xml中使用<many-to-one> 并指定unique=true people.hbm.xml: < ...
- Linux 进程通信之:内存共享(Shared Memory)(转,好文章)
https://blog.csdn.net/afei__/article/details/84188548
- Codeforces Round #538 (Div. 2) (CF1114)
Codeforces Round #538 (Div. 2) (CF1114) 今天昨天晚上的cf打的非常惨(仅代表淮中最低水平 先是一路缓慢地才A掉B,C,然后就开始杠D.于是写出了一个O( ...
- STM32F103单片机学习—— 通用定时器
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fengshuiyue/article/details/79150724 本篇重点记录的是STM32F ...