Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心
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 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.
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).
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.
题解转自:http://www.cnblogs.com/iwtwiioi/p/3961709.html
题意:给你一个没有长度为2以上(包括2)的回文串的一个字符串,然后让你求比他大的所有排列且没有长度为2以上(包括2)的下一个最小的排列。
剩下的1个半小时都在搞这题。。。。。。。。。。T_T
神题。。
我dfs写渣
判重写渣
然后滚粗。
这里有个贪心。
串的任意部分都不能是回文=串中任意连续3个字符互不相同
那么如果从左往右找,那么只需要判断x-1和x-2与x不等即可
其次我们要保证字典序最小,所以我们从右向左依次尝试修改,只要找到不符合回文条件的点,就修改它。这样保证了前边的最小。。
而且能证明,后边一定能构造出一个不是回文的串。例如m=4,我可以构造abcabcabc。。。。。。。。。。。。(x-1和x-2与x不等)
程序中有访问 char 数组的负下标,感觉太高大上了,我试了下,负下标的%d输出为0
网上有人说原因是 :记住,下标只是个操作符。 x[n] 等价于 *(x + n)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 1005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,p;
char s[N]; char ok(int t)
{
char i;
for(i=s[t]+;i-'a'+<=p;i++){
if(s[t-]!=i && s[t-]!=i) return i;
}
return ;
} int solve()
{
int i;
char t;
// if(p==1) return 0;
// if(n==1) return
for(i=n-;i>=;i--){
t=ok(i);
if( t!= ){
s[i]=t;
break;
}
}
if(i==-) return ; i++;
for(;i<n;i++){
s[i]='a'-;
t=ok(i);
if( t!= ){
s[i]=t;
}
}
return ;
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&p)!=EOF)
{ scanf("%s",s);
// if(p==1){
// printf("NO\n");continue;
// }
if(solve()==){
printf("NO\n");
}
else{
printf("%s\n",s);
}
} return ;
}
Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心的更多相关文章
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #265 (Div. 2) D. Restore Cube 立方体判断
http://codeforces.com/contest/465/problem/D 给定8个点坐标,对于每个点来说,可以随意交换x,y,z坐标的数值.问说8个点是否可以组成立方体. 暴力枚举即可, ...
- Codeforces Round #265 (Div. 2) D. Restore Cube 立方体推断
http://codeforces.com/contest/465/problem/D 给定8个点坐标.对于每一个点来说,能够任意交换x.y,z坐标的数值. 问说8个点能否够组成立方体. 暴力枚举就可 ...
- Codeforces Round #265 (Div. 2)
http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...
- Codeforces Round #265 (Div. 1) C. Substitutes in Number dp
题目链接: http://codeforces.com/contest/464/problem/C J. Substitutes in Number time limit per test 1 sec ...
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...
- Codeforces Round #265 (Div. 2) E. Substitutes in Number
http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定 ...
随机推荐
- Luogu P4593 [TJOI2018]教科书般的亵渎
亵渎终于离开标准了,然而铺场快攻也变少了 给一个大力枚举(无任何性质)+艹出自然数幂和的方法,但是复杂度极限是\(O(k^4)\)的,不过跑的好快233 首先简单数学分析可以得出\(k=m+1\),因 ...
- 2006: C语言实验——拍皮球
2006: C语言实验——拍皮球 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 231 Solved: 162[Submit][Status][Web ...
- 菜鸟教你如何通俗理解——>集群、负载均衡、分布式
在“高并发,海量数据,分布式,NoSql,云计算......”概念满天飞的年代,相信不少朋友都听说过甚至常与人提起“集群,负载均衡”等,但不是所有人都有机会真正接触到这些技术,也不是所有人都真正理解了 ...
- 用事件队列解决GUI的操作顺序问题(Qt中处理方法)
GUI操作顺序问题引发异常: 有时候我们使用写GUI程序的时候会遇到这样的问题:比如在程序中,建立了一个列表的GUI.这个列表是随着时间不断更新的,而且操作也会读取这个列表GUI的内容. 如果这个程序 ...
- 人脸识别源代码Open cv
#include <stdio.h> #include <string.h> #include "cv.h" #include "cvaux.h& ...
- ★房贷计算器 APP
一.目的 1. 这是一个蛮有用的小工具 2. 之前看了很多demo,第一次来完全的自己实现一个APP 3. 完成之后提交 App Store 4. 作为Good Coder的提交审核材料 二.排期 周 ...
- grep-sed命令用法:
用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND echo -n ...
- 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告
目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...
- Springboot(一)-IDEA搭建springboot项目(demo)
jdk版本:1.8.0_162 1.打开IDEA-file-new-project-Spring Initializer,JDK和URL选默认,next (这一步如果是不能联网的话,可以选择直接创建m ...
- ubuntu 执行apt-get update报错Failed to fetch
在ubuntu下执行sudo apt-get update时,经常会遇到报错: Err http://security.ubuntu.com precise-security InRelease Er ...