Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:55:36
原创
1380人浏览过

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;}
登录后复制


立即学习前端免费学习笔记(深入)”;

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号