题目连接:
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;
}