topic link : [Kuangbin take you to fly] Special topic 16 KMP & Extension KMP & Manacher
Test instructions
A string s consisting of a lowercase English character a,b,c...y,z is given, and the length of the longest palindrome in S is obtained.
Palindrome is the same as the inverse of the string, such as ABA, ABBA, etc.
Ideas
With a special character inserted into the S string in the middle of every two characters, the implementation of each palindrome string is an odd number, and then the manacher algorithm to solve.
Code
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace STD;Const intN =110009;CharS[n], t[n<<1];intp[n<<1];intManacher (intLen) {intID =1, mx=0; p[0] =1; for(intI=1; i<len; i++) {if(mx > i) p[i] = min (mx-i, p[2*id-i]);ElseP[i] =1; while(T[i+p[i]] = = T[i-p[i]]) p[i]++;if(MX < p[i]+i) {mx = p[i]+i; id = i; } }intAns = p[1]; for(intI=1; i<len; i++)if(Ans < p[i]) ans = p[i];returnans-1;}intMain () { while(~scanf('%s ', s)) {t[0] =' @ '; t[1] =' # ';intLen =strlen(s); for(intI=0; i<len; i++) {t[i*2+2] = S[i]; s[n*2+3] =' # '; }printf("%d\n", Manacher (len*2+2)); }return 0;}
HDU 3068 longest palindrome (Manacher)