http://acm.hdu.edu.cn/showproblem.php?pid=5340
/*manacher algorithm: O (n) Implementation of the longest palindrome substring algorithm implementation: first inserted into the original string does not exist characters, generally with ' # ', then O (n) traversal again, with an array p[i] to record the palindrome radius (note str[i] is a new string, Length is 2*len+1), MX records the current existing palindrome the longest position, assuming that the current i,j for I have already calculated number, ID is the longest palindrome center, then you can get an equation p[i] = min (p[2*id-i],mx-i), p[2*id-i] and P[i ] is about the ID symmetry, then p[i] palindrome length is p[2*id-1] palindrome length, but if p[i] the length of the p[id] of the MX, then the right is not sure, and then expand outward *//**************************** Author:P owatr* Created time:2015-8-12 13:11:42* File Name:Manacher.cpp ************** /#include <cstdio> #include <algorithm> #include <iostream># Include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef Long Ll;constint MAXN = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int PRE1[MAXN], pre2[maxn];int P[maxn];char S[MAXN] ; Char str[maxn];int n;void inti () {memset (str, 0, sizeof (str)); memset (p, 0, sizeof (p)); memset (pre1, 0, sizeof (pre1)); memset (pre2, 0, sizeof (pre2)); n = strlen (s+1); Str[0] = ' $ '; for (int i = 1; I <= n; i++) {str[i*2-1] = ' # '; STR[I*2] = s[i]; } n = 2*n+1; Str[n] = ' # '; str[n+1] = ' + ';} void Manacher () {int mx = 0, id; for (int i = 1; I <= n; i++) {if (mx > i) p[i] = min (p[2*id-i], mx-i); else p[i] = 1; for (; Str[i+p[i]] = = Str[i-p[i]]; p[i]++); if (P[i] + i > mx) {mx = p[i] + i; id = i; }}}bool Check () {int cout1 = 0, cout2 = 0; for (int i = 1; I <= n; i++) {if (p[i] = = I && i! = 1) {//printf ("%d", I); PRE1[++COUT1] = i;} if (i + p[i]-1 = = N && i! = N) pre2[++cOUT2] = i; } int L, r, Mid; for (int i = 1, i <= cout1; i++) {for (int j = 1; J <= Cout2; j + +) {L = Pre1[i] + p[pre1[i]]; r = pre2[j]-p[pre2[j]]; if (L > R) continue; MID = L + R >> 1; if (P[mid] > (r-l + 1 >> 1)) {//This side is greater than, because P[mid] contains itself return true; }}} return false;} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%s", s+1); n = strlen (s+1); if (n < 3) {printf ("no\n"); continue;} Inti (); Manacher (); if (check ()) printf ("yes\n"); else printf ("no\n"); } return 0;}
Hdu5340--manacher algorithm--three palindromes