Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5340
Test instructions: Determine whether a string can be divided into three palindrome strings
Solution: Manacher enumerates the first third, judging the second one.
Code:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <string>#include <assert.h>using namespace STD;intTintp[220100];Chars[220100], c[221000];intpre[221000], last[221000];intSolve () {intLen =strlen(s); c[0] =' $ '; for(inti =0; i<len; i++) C[i *2+1] =' # ', C[i *2+2] = S[i]; C[len *2+1] =' # '; Len = Len *2+2; C[len] =0;intMX =0, id =0; for(inti =1; i<len; i++) {if(mx>i) P[i] = min (P[id *2-i], P[id] + id-i);ElseP[i] =1; while(C[i + p[i]] = = C[i-p[i]]) p[i]++;if(i + p[i]>mx) mx = i + p[i], id = i; }intP_num =0, L_num =0; for(inti =2; I < Len-1; i++) {if(i = = P[i]) pre[p_num++] = i;if(i + p[i] = = len) last[l_num++] = i; } for(inti =0; i < P_num; i++) { for(intj = L_num-1; J >=0; j--) {intPOS1 = Pre[i] + p[pre[i];intPos2 = Last[j]-p[last[j];if(Pos1 > Pos2) Break;intMid = (pos1 + pos2)/2;if(mid<=len-4)if(MID-POS1 +1<= P[mid])return 1; } }return 0;}intMain () {scanf("%d", &t); while(t--) {scanf('%s ', s);if(Solve ())printf("yes\n");Else printf("no\n"); }return 0;}
Copyright NOTICE: Reprint please indicate the source.
HDU 5340 three palindromes "Manacher"