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.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#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\n");
            continue;
        }
        if(n == 2) { 
            printf("1 : NO\n");
            printf("2 : ab\n");
            continue;
        }
        printf("1 : NO\n");
        printf("2 : NO\n");

        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("\n");
        }
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5746757.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值