zoukankan      html  css  js  c++  java
  • URAL 2037 Richness of binary words (回文子串,找规律)

    Richness of binary words

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/126823#problem/B

    Description

    For each integer i from 1 to n, you must print a string s i of length n consisting of letters ‘a’ and ‘b’ only. The string s i must contain exactly i distinct palindrome substrings. Two substrings are considered distinct if they are different as strings.

    Input

    The input contains one integer n (1 ≤ n ≤ 2000).

    Output

    You must print n lines. If for some i, the answer exists, print it in the form “ i : s i” where s i is one of possible strings. Otherwise, print “ i : NO”.

    Sample Input

    input output
    4
    1 : NO
    2 : NO
    3 : NO
    4 : aaaa


    ##题意: 要求输出n个长度为n的字符串(只能放a或b): 要求Si中不同回文子串的个数恰为i.
    ##题解: 本质就是找出一种构造方式使得回文子串个数不会再增加. 参考2015-ICPC合肥现场赛H题: http://blog.csdn.net/snowy_smile/article/details/49870109 http://blog.csdn.net/keshuai19940722/article/details/49839359
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 210000 #define mod 100000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

    int n;

    int main(int argc, char const *argv[])
    {
    //IN;

    while(scanf("%d", &n) != EOF)
    {
        if(n == 1) {
            printf("1 : a
    ");
            continue;
        }
        if(n == 2) {
            printf("1 : NO
    ");
            printf("2 : ab
    ");
            continue;
        }
    
        if(n < 8) {
            for(int i=1; i<n; i++)
                printf("%d : NO
    ", i);
            printf("%d : ", n);
            for(int i=1; i<=n; i++)
                putchar('a');
            printf("
    ");
            continue;
        }
    
        if(n == 8) {
            for(int i=1; i<7; i++)
                printf("%d : NO
    ", i);
            printf("7 : aababbaa
    ");
            printf("8 : ");
            for(int i=1; i<=n; i++)
                putchar('a');
            printf("
    ");
            continue;
        }
    
        char str[] = "abaabb";
        for(int i=1; i<=7; i++)
            printf("%d : NO
    ", i);
        for(int i=8; i<=n; i++) {
            printf("%d : ", i);
            for(int j=1,cur=0; j<=n; j++) {
                if(j <= i-8) putchar('a');
                else {
                    if(cur == 6) cur = 0;
                    putchar(str[cur++]);
                }
            }
            printf("
    ");
        }
    }
    
    return 0;
    

    }

  • 相关阅读:
    CentOS7安装mysql
    strusts2的开发包
    CentOS7配置mavne国内镜像
    卸载linux自带的jdk
    Centos7安装nodejs
    redis启动方式
    bash学习笔记——Shell变量
    bash学习笔记——bash是什么
    教育管理系统——android家长客户端
    php入门学习——php与jsp对比
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5746764.html
Copyright © 2011-2022 走看看