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个点是否可以组成立方体. 暴力枚举即可, ...
随机推荐
- IIS 域名 带参数 设置重定向
IIS里面设置重定向后,经常会出现,从百度快照里直接打不开的情况. 可以在IIS里面设置重定向的时候,把参数加上,格式如下: http://www.***.com%S%Q
- (转)silverlight应用程序中未处理的错误代码:2104 类别:InitializeError
解决方案:第一步:默认网站--属性-----http头 第二步:点击mime类型: 第三步:点击新建: 第四步:输入扩展名以及类型: (1) 扩展名:.xaml MIME类型:applicat ...
- Java基础之—反射
反射是框架设计的灵魂 (使用的前提条件:必须先得到代表的字节码的Class,Class类用于表示.class文件(字节码)) 一.反射的概述 JAVA反射机制是在运行状态中,对于任意一个类,都能够 ...
- maven的父工程中添加子工程
父工程的结构如下: 1.选中父工程名,接着单击鼠标右键,选择”Maven“ -----> "New Maven Module Project". 2.如下图,打勾 ---&g ...
- windows mysql 主从热备
环境说明: Master:192.168.1.200 Slave:192.168.1.210 MySQL 的 Master 配置: 配置my.ini: [mysqld] # T ...
- appium获取package和activity
输入命令adb shell dumpsys window w |findstr \/ |findstr name=然后在手机端打开app,电脑上按enter就即可显示 以微信为例子 appPackag ...
- 软件工程网络15个人作业4--Alpha阶段个人总结
一.个人总结 在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 请用自我评价表:http://www.cnblogs.com/xinz/p/3852177.html 有 ...
- Hadoop3集群搭建之——hive添加自定义函数UDTF
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- 2018.10.30 NOIP模拟 字胡串(单调栈+容斥)
传送门 对于每个点,用单调栈求出它左右第一个比他大的位置. 然后对每个点O(logai)O(log_{a_i})O(logai)求出第一个拥有跟它不同二进制位的位置. 然后容斥一下就行了. 代码
- 牛客训练六:海啸(二维树状数组+vector函数的使用)
题目链接:传送门 思路: 二维树状数组, vector(first,last)函数中assign函数相当于将first中的函数清空,然后将last中的值赋值给first. 参考文章:传送门 #incl ...