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.
##代码: ``` cpp #include