Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
http://codeforces.com/contest/465/problem/C
给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定范围内,并且说同样不存在长度大于2的回文子串。
直接去递归构造即可,从最后一位开始,每次只要判断是否子串中含有回文串,其实仔细想想只要考虑是否存在一个字符和前两个字符中的一个相同即可。不卡时限,裸的判断都能过
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include<set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
int p,n;
char ch[1005],ans[1005];
//int fun(int low, int high, char *str, int length)
//{
// if (length == 0 || length == 1)
// return 1;
// if (str[low] != str[high])
// return 0;
// return fun(low+1, high-1, str, length-2);
//}
int fun(int low, int high, char *str, int length)
{
if (length == 0 || length == 1)
return false;
if(str[low] == str[low+1])
return true;
for(int i = low + 2;i <= high;++i){
if(str[i] == str[i-1] || str[i] == str[i-2])
return true;
}
return false;
}
bool find(int x,bool big)
{
if(x == n){
if(strcmp(ch,ans) == 0)
return false;
return true;
}
for(char i = big?'a':ch[x];i < p+'a';++i){
ans[x] = i;
int j;
if(fun(0,x,ans,x+1)) continue;
// for(j = 0;j < x;++j)
// if(fun(j,x,ans,x-j+1))
// break;
// if(j != x) continue;
if(find(x+1,big|ans[x] > ch[x]))
return true;
}
return false;
}
int main() {
RD2(n,p);
scanf("%s",ch);
ans[n] = '\0';
if(find(0,0))
puts(ans);
else
puts("NO");
return 0;
}
Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串的更多相关文章
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #581 (Div. 2)D(思维,构造,最长非递减01串)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100007];int main ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- 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) E. Substitutes in Number
http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...
- Codeforces Round #265 (Div. 2) D. Restore Cube 立方体判断
http://codeforces.com/contest/465/problem/D 给定8个点坐标,对于每个点来说,可以随意交换x,y,z坐标的数值.问说8个点是否可以组成立方体. 暴力枚举即可, ...
随机推荐
- dpdk优化相关 转
注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...
- 探索未知种族之osg类生物---起源
任何程序都是有生命的,是生命就需要呼吸.例如普通的windows程序,当运行完main()函数后,就需要进入消息循环,来监听用户的各种操作,以便做出及时的回应.这样的每次循环就像生命的每次呼吸,来维持 ...
- codeforces 数字区分 搜索
Jokewithpermutation Input file: joke.inOutput file: joke.outJoey had saved a permutation of integers f ...
- 拍照一分钟,修图两小时,PS大神是这样修片的!
乌克兰有一个叫Viktoria Solidarnyh的美图艺术家,这个艺术家有一个特别的技能——P图,她P的图,水平真的非常赞...来感受一下.... 瞬间变成魔幻田园风... 编辑:千锋UI设计 ...
- [SoapUI] 通过JSONAssert比较两个环境的JSON Response,定制化错误信息到Excel
package ScriptLibrary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSO ...
- python正则表达式获取两段标记内的字符串
比如获取绿色字符串 ModelData.PayTableData =[{"}, {"}, {"}]; ModelData.PayTableData1 =[{"} ...
- 跟我学Spring Boot(三)Spring Boot 的web开发
1.Web开发中至关重要的一部分,Web开发的核心内容主要包括内嵌Servlet容器和SpringMVC spring boot 提供了spring-boot-starter-web 为web开发提 ...
- Peter的烟(水题测试2017082401&洛谷1150)
题目链接:Peter的烟 这道题基本做法很水,不解释. #include<bits/stdc++.h> using namespace std; int main(){ int n,k; ...
- Unity3d 插值Lerp与Slerp
Lerp Lerp表示线性插值. Slerp 球形插值在Vector3.Quaternion等类都有使用,一般多在Quaternion的旋转操作时使用.
- Ubuntu 中用 delphi 开发 apache
经过近15年的沉默.delphi 10.2 终于重新开始支持linux 开发了. 今天说一下在ubuntu中开发apache的方法. 首先安装ubuntu 的delphi 开发环境,请参考以前的文章 ...