Ural 2037. Richness of binary words 打表找规律 构造

2037. Richness of binary words

题目连接:

http://acm.timus.ru/problem.aspx?space=1&num=2037

Description

For each integer i from 1 to n, you must print a string si of length n consisting of letters ‘a’ and ‘b’ only. The string si 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 : si” where si is one of possible strings. Otherwise, print “i : NO”.

Sample Input

4

Sample Output

1 : NO
2 : NO
3 : NO
4 : aaaa

Hint

题意

让你构造长度为n,字符集为2,且本质不同的回文串恰好i个的字符

无解输出no

题解:

打表找规律,比较容易发现答案其实就是aababb->aaababb->aaaababb的循环,这样的。

然后输出就好了。

代码

#include <bits/stdc++.h>

using namespace std;
int N;

void solve_special(){
    if( N <= 7 ){
        for(int i = 1 ; i < N ; ++ i) printf("%d : NO\n" , i);
        printf("%d : ",N);
        for(int j = 1 ; j <= N ; ++ j) putchar('a');
        puts("");
    }else if( N == 8 ){
        cout << "1 : NO" << endl;
        cout << "2 : NO" << endl;
        cout << "3 : NO" << endl;
        cout << "4 : NO" << endl;
        cout << "5 : NO" << endl;
        cout << "6 : NO" << endl;
        cout << "7 : aababbaa" << endl;
        cout << "8 : aaaaaaaa" << endl;
    }else if( N == 9 ){
        cout << "1 : NO" << endl;
        cout << "2 : NO" << endl;
        cout << "3 : NO" << endl;
        cout << "4 : NO" << endl;
        cout << "5 : NO" << endl;
        cout << "6 : NO" << endl;
        cout << "7 : NO" << endl;
        cout << "8 : aaababbaa" << endl;
        cout << "9 : aaaaaaaaa" << endl;
    }else if( N == 10 ){
        cout << "1 : NO" << endl;
        cout << "2 : NO" << endl;
        cout << "3 : NO" << endl;
        cout << "4 : NO" << endl;
        cout << "5 : NO" << endl;
        cout << "6 : NO" << endl;
        cout << "7 : NO" << endl;
        cout << "8 : aaababbaaa" << endl;
        cout << "9 : aaaababbaa" << endl;
        cout << "10 : aaaaaaaaaa" << endl;
    }
}

void solve(){
    int target = (N-11)/2 + 2 ;
    for(int i = 1 ; i < 8 ; ++ i){
        printf("%d : NO\n" , i);
    }
    int cur = 2;
    for(int i = 8 ; i < N ; ++ i){
        int len = 0 , flag = 0 , rs = cur;
        printf("%d : " , i);
        while( len < N ){
            if( flag == 0 ){
                putchar('a');
                -- rs;
            }else if( flag == 1 ){
                if( rs == 3 ) putchar('a');
                else putchar('b');
                -- rs;
            }
            if( rs == 0 ){
                flag ^= 1;
                if( flag == 0 ) rs = cur;
                else rs = 4;
            }
            ++ len;
        }
        if( cur == target ) cur += 2;
        else ++ cur;
        puts("");
    }
    printf("%d : ",N);
    for(int i = 1 ; i <= N ; ++ i) putchar('a');
    puts("");
}

int main(int argc,char *argv[]){
    //freopen("KO.txt","w",stdout);
    scanf("%d",&N);
    if( N < 10 ) solve_special();
    else if( N == 10 ){
        cout << "1 : NO" << endl;
        cout << "2 : NO" << endl;
        cout << "3 : NO" << endl;
        cout << "4 : NO" << endl;
        cout << "5 : NO" << endl;
        cout << "6 : NO" << endl;
        cout << "7 : NO" << endl;
        cout << "8 : aaababbaaa" << endl;
        cout << "9 : aaaababbaa" << endl;
        cout << "10 : aaaaaaaaaa" << endl;
    }else solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值