Question
Refer to blog http://blog.csdn.net/u013368721/article/details/37575165
// Visit [26] [26] [26] [26] [26] is used to determine whether the four-character string of the first three child rivers in the new home district is the same as the previous one. // The third character is added, so the total length is 26*26*26*26 + 3. /* // Copy the following content from the blog. The following conditions must be met: 1. the string length is N. 2. This string contains only lowercase letters. 3. a substring with a length greater than or equal to 4 can only appear once. If there is no solution, output impossible. Question Analysis: we have come up with conjecture .. Because a substring greater than or equal to 4 can only appear once, the maximum length of the substring is 26*26*26*26 + 3 = 456979. At first, I thought of the AC automatic machine. I wanted to go back to the root every three nodes on the AC automatic machine .. However, this simply cannot be done =, so another amazing method was created-each time a letter is added ~ Z for again, check whether this letter and the first three letters of the Child string has appeared in the previous string, if not, the next position is determined from the next letter. If the same substring already exists, the next letter is enumerated, if you still cannot add a letter to the current position until Z, but this time ~ If there is at least one added in Z to the previous position, re-start and then, otherwise, the loop is ended (because all 26 letters have been enumerated and the substrings that contain the first three letters have appeared ). At the beginning, we set AAA as the starting string, and then output the final string length to 456954! Infinitely close, and then the output is not added to the letter in the string. In fact, except for a, all four identical letters, such as BBBB and CCCC, are not added... Then I put them all in front and output them again. The maximum length of the string is exactly 456979! Solve the problem. (Now I understand that the length of the primary string that appears only once can reach (26 ^ 4) + 3) PS: Can Be bold conjecture, the maximum length of the primary string that can contain X different characters is equal to or greater than m (M <= X) + m-1! Prove to be future generations. */# Include <stdio. h> # include <string. h ># include <string> int n = 26*26*26*26 + 3; char s [500010] = "bytes "; int vis [26] [26] [26] [26]; void Init () {memset (VIS, 0, sizeof (VIS); int Len = 26*4; int flag = 1; for (INT I = 3; I <Len; I ++) {vis [s [I-3]-'a'] [s [I-2]-'a'] [s [I-1]-'a'] [s [I]-'a '] = 1 ;} while (FLAG) {flag = 0; For (INT I = 0; I <26; I ++) {If (! Vis [s [len-3]-'a'] [s [len-2]-'a'] [s [len-1]-'a'] [I]) {s [Len] = I + 'a '; vis [s [len-3]-'a'] [s [len-2]-'a'] [s [len-1]-'a'] [I] = 1; len ++; flag = 1 ;}}} int main () {int N, Len; Init (); While (~ Scanf ("% d", & N) {If (n> N) printf ("impossible"); else {for (INT I = 0; I <N; I ++) printf ("% C", s [I]);} puts ("");} return 0 ;}
View code