The principle is clear: manacher's algorithm: O (n) time is used to obtain the longest substring of a string.
Note:
① After the dynamic life P [] and newstr arrays, do not forget to delete [] // In fact, this is the basic encoding habit.
② The final returned result is P [I]-1.
The manacher function written by myself is as follows:
Int manacher (char * SRC) {int srclen = strlen (SRC); int Len = 2 * srclen + 2; char * newstr = new char [Len]; // or + 3 ?? Do you want to leave a position for \ 0 ?? Int * P = new int [Len]; int I, RST = 0; // process the string newstr [0] = '$'; newstr [1] = '#'; for (I = 0; I <srclen; I ++) {newstr [2 * I + 2] = SRC [I]; newstr [2 * I + 3] = '#';} // key step: Calculate P [I] int id = 0; int MX = 0; P [0] = 1; for (I = 1; I <Len; I ++) {If (MX> I) P [I] = min (Mx-I, P [2 * ID-I]); else P [I] = 1; while (newstr [I + P [I] = newstr [I-P [I]) P [I] ++; // It is said that this step will never be set up // update ID and MX if (P [I] + I> MX) {id = I; MX = P [I] + I ;}// find the maximum value in P [I]. Note that the final returned result is P [I]-1 for (I = 0; I <Len; I ++) if (RST <p [I]) rst = P [I]; Delete [] P; Delete [] newstr; return rst-1 ;}
Reference code: http://blog.csdn.net/ggggiqnypgjg/article/details/6645840 with a fixed-length char array
Http://blog.csdn.net/bruce_zeng/article/details/8629572 new dynamic declaration without Delete
Http://blog.csdn.net/pi9nc/article/details/9251455
[Retrieval string] Longest retrieval substring O (n) manacher Algorithm