Link:
Http://poj.org/problem? Id = 2541
Analysis and Summary:
I have estimated the complexity of this question. I feel that no matter how fast the KMP is, this question must have timed out.
After thinking for a long time, I didn't come up with a good solution, so I decided to be violent, but TLE was just a few days away.After reading discuss, the positive solution of this question should be status compression DP, but at present I still don't understand, kneel down.
After that, Baidu found that KMP can also be used for water use. Although it was because of data water, this idea is clever and worth learning!
The brute force is to enumerate the 13 letters behind the string, and then use KMP to match the string. In this case, you need to enumerate the strings multiple times, which are the 13, 12, 11 .... 1 letter. However, we can see that what is actually required is the longest public suffix! You can convert the original string in reverse order to the longest public prefix! This requires only one request. It is enough to match the first 13 letters of the string with the original string!
Analysis and Summary:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int maxn = 1002000; const int start = 2000; char T [maxn]; int f [maxn]; int n, l; inline char getfail (char * P, int Len) {int n = strlen (P ); f [0] = f [1] = 0; int Pos =-1, max =-1; for (INT I = 1; I <n; ++ I) {Int J = f [I]; while (J & P [I]! = P [J]) J = f [J]; F [I + 1] = P [I] = P [J]? 1 + J: 0; If (F [I] = 13) {return P [I-f [I]-1];} If (max <F [I]) max = f [I], Pos = I-f [I]-1;} If (max =-1) return '0'; return P [POS];} int main () {While (scanf ("% d % * C", & N, & L )! = EOF) {char * P; char * STR = T + start; P = STR; gets (STR); // converts int Len = strlen (STR) in reverse order ); for (INT I = 0, j = len-1; I <Len/2; ++ I, -- j) {char T = STR [I]; STR [I] = STR [J]; STR [J] = T;} For (INT I = 0; I <L; ++ I) {* (str-1) = getfail (STR, Len); -- STR; ++ Len;} -- p; while (P> = Str) {putchar (* P); -- p ;} puts ("");} return 0 ;}
-- The significance of life is to give it a meaningful person.
Original Http://blog.csdn.net/shuangde800,By
D_double (reprinted please mark)