1089 maximum echo substring V2 (manacher algorithm) reference time limit: 1 second space limit: 131072 kb score: 0 difficulty: A base-question echo string is a symmetric string between the left and right sides of ABA, Abba, cccbccc, and AAAA. Input a string 'str' and output the maximum length of the substring in 'str. Input
Input STR (STR length <= 100000)
Output
The maximum length of the input string (l.
Input example
daabaac
Output example
5
Marathon algorithm Template
It is also a further understanding of the marathon algorithm.
1 // marathon algorithm 2 # include <bits/stdc ++. h> 3 # define n 1000000 4 using namespace STD; 5 6 int reslen; 7 int P [N]; 8 int manacher (string S) {9 // Insert '#'10 string T = "$ #"; 11 for (INT I = 0; I <S. length (); ++ I) {12 t + = s [I]; 13 T + = "#"; 14} 15 // process T16 int MX = 0, id = 0, reslen = 0, rescenter = 0; 17 for (INT I = 1; I <t. length (); ++ I) {18 // assign values to the substrings that have been repeatedly searched, and analyze 19 P [I] = Mx> I? Min (P [2 * ID-I], Mx-I): 1; 20 // multiple parts are analyzed by yourself 21 While (T [I + P [I] = T [I-P [I]) 22 + P [I]; 23 24 // update the maximum position to the right and record the current position 25 if (MX <I + P [I]) {26 MX = I + P [I]; 27 id = I; 28} 29 // update the length of the oldest string, and the length of the radius and current position 30 if (reslen <p [I]) {31 reslen = P [I]; 32 // rescenter = I; 33} 34} 35 // return S. substr (rescenter-reslen)/2, reslen-1); 36 return reslen-1; 37} 38 39 string s; 40 int main () {41 CIN> S; 42 cout <manacher (s) <Endl; 43 return 0; 44}
1089 longest reply substring