Hdoj 3068 longest retrieval [manacher algorithm ],
Meaning...
In the traditional method, timeout is required (that is, parity is required ).
Manacher algorithms: Baidu has explained a lot.
Commemorative code:
# Include <stdio. h> # include <string. h >#include <algorithm> using namespace std; # define M 110010 char a [M], B [M <1]; int p [M <1]; int main () {while (~ Scanf ("% s", & a [1]) {// int len = strlen (a); int I, len; for (I = 1; a [I]! = '\ 0'; I ++) {B [I <1] = a [I]; B [(I <1) + 1] = '#';} len = (I <1) + 2; B [0] = '@'; B [1] = '#'; B [len] = 0; // printf ("% d % s. ", len, & B [1]); int maxid, id, max; maxid = max = id = 0; for (I = 1; I <len; I ++) {if (maxid> I) {p [I] = min (p [2 * id-I], maxid-I);} else p [I] = 1; while (B [I-p [I] = B [I + p [I]) ++ p [I]; if (maxid <p [I] + I) {// each maxid <p [I + I] updates the maxid and id. Maxid = p [I] + I; id = I;} if (max <p [I]) max = p [I]; // printf ("% d .. ", p [I]);} printf (" % d \ n ", max-1);} return 0 ;}
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 3068