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


代码:

#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;
        }

        if(n < 8) {
            for(int i=1; i<n; i++)
                printf("%d : NO\n", i);
            printf("%d : ", n);
            for(int i=1; i<=n; i++)
                putchar('a');
            printf("\n");
            continue;
        }

        if(n == 8) {
            for(int i=1; i<7; i++)
                printf("%d : NO\n", i);
            printf("7 : aababbaa\n");
            printf("8 : ");
            for(int i=1; i<=n; i++)
                putchar('a');
            printf("\n");
            continue;
        }

        char str[] = "abaabb";
        for(int i=1; i<=7; i++)
            printf("%d : NO\n", 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("\n");
        }
    }

    return 0;
}

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值