zoukankan      html  css  js  c++  java
  • URAL 2045 Richness of words (回文子串,贪心)

    Richness of words

    题目链接:

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

    Description

    For each integer i from 1 to n, you must print a string s i of length n consisting of lowercase Latin letters. 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 : abca
    4 : bbca


    ##题意: 要求输出n个长度为n的字符串(可以放26个小写字母): 要求Si中不同回文子串的个数恰为i.
    ##题解: 首先容易明确:n个相同字符组成的字符串的回文子串恰有n个. 那么对于小于n的情况,要想办法在上述基础上减少不同回文子串的个数. 减少的方式是使其出现重复的串. 而为了使得重复的串之间不构成新得回文串,必须插入不相同的字符. 可以证明,当插入连续的"bc"后,无论怎样都不可能构成新的回文串. 对于Si: 先输出i-2个'a',再输出"bc", 此时的i个字符恰好构成i个回文字串,为了使后面的字符不构成新回文串,可以不断填充"abc". 注意,此题时限比较严格,容易TLE.
    ##代码: ``` 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;
        }
        printf("1 : NO
    ");
        printf("2 : NO
    ");
    
        for(int i=3; i<=n; i++) {
            printf("%d : ", i);
            for(int j=1; j<=i-2; j++) putchar('a');
            printf("bc");
    
            for(int j=i+1,k=0; j<=n; j++,k=(k+1)%3) {
                if(k==0) putchar('a');
                if(k==1) putchar('b');
                if(k==2) putchar('c');
            }
            printf("
    ");
        }
    }
    
    return 0;
    

    }

  • 相关阅读:
    MiniUI 在线示例(gridview)
    数据库的事务处理和并发控制
    数据库大并发操作要考虑死锁和锁的性能问题
    c# 财务报表数字转大写的方法
    js 实现打印功能1
    js 实现打印功能
    《C++ Primer 4th》读书笔记 序
    谈谈Vim中实用又好记的一些命令
    《Unix网络编程》卷2 读书笔记 第3章- System V IPC
    《Unix网络编程》卷2 读书笔记 第2章- Posix IPC
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5746757.html
Copyright © 2011-2022 走看看