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
题意: 让你构造长度为n,字符集为2,且本质不同的回文串恰好i个的字符。
分析:打表发现abaa可以很好的截断回文串,然后分类讨论构造答案。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 10005
using namespace std;
int n;
char s[MAXN],p[MAXN];
int main()
{
scanf("%d",&n);
if(n <= 7)
{
for(int i = 1;i < n;i++) cout<<i<<" : NO"<<endl;
for(int i = 0;i < n;i++) s[i] = 'a';
s[n] = '\0';
cout<<n<<" : "<<s<<endl;
return 0;
}
if(n == 8)
{
for(int i = 1;i < 7;i++) cout<<i<<" : NO"<<endl;
cout<<7<<" : bbabaabb"<<endl;
cout<<8<<" : "<<"aaaaaaaa"<<endl;
return 0;
}
for(int i = 1;i < 8;i++) cout<<i<<" : NO"<<endl;
p[0] = 'b',p[1] = 'b',p[2] = 'a',p[3] = 'b',p[4] = 'a',p[5] = '\0';
for(int i = 8;i < n;i++)
{
int leng = strlen(p);
p[leng] = 'a';
p[++leng] = '\0';
for(int j = 0;j < n;j++) s[j] = p[j % leng];
s[n] = '\0';
cout<<i<<" : "<<s<<endl;
}
for(int i = 0;i < n;i++) s[i] = 'a';
s[n] = '\0';
cout<<n<<" : "<<s<<endl;
}