Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
2015-09-21 09:28
435 查看
http://codeforces.com/contest/465/problem/C
给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定范围内,而且说相同不存在长度大于2的回文子串。
直接去递归构造就可以。从最后一位開始。每次仅仅要推断是否子串中含有回文串,事实上细致想想仅仅要考虑是否存在一个字符和前两个字符中的一个同样就可以。不卡时限,裸的推断都能过
给定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 = '\0'; if(find(0,0)) puts(ans); else puts("NO"); return 0; }
相关文章推荐
- Javascript:谈谈JS的全局变量跟局部变量
- JS实现超简单的仿QQ折叠菜单效果
- Linux常用命令大全
- Unity3D光照贴图烘焙知识详解
- RegexKitLite的配置
- 重磅来袭,使用CRL实现大数据分库分表方案
- python中正则表达式以及re模块的使用
- 使用 RequireJS 优化 Web 应用前端
- 【Modern Business Process Automation】现代业务流程自动化:Day1.基础
- soa方面的一些东西
- 打包apk并安装时,显示安装成功但是找不到安装图标
- Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法
- Android笔记之启动包的最适合Activity
- 推荐系统评测指标—准确率(Precision)、召回率(Recall)、F值(F-Measure)
- PHP追加写文件
- DIV+CSS规范命名大全
- 在C++遇到有些关键字或者函数被弃用的情况
- 09.21,枚举
- 程序员保值的4个秘密
- 多路径路由算法选择(6)——最大流最小割定理