Three palindromes
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1244 Accepted Submission (s): 415
Problem Descriptioncan We divided a given string S into three nonempty palindromes?
Inputfirst line contains a single integerT≤ which denotes the number of test cases.
For each test case, the there is a single line contains a string S which only consist of lowercase Chinese letters.1≤| S| ≤20000
Outputfor each case with the output the "Yes" or "No" in a.
Sample Input2abcabaadada
Sample outputyesno topic: Ask if you can find the three-segment palindrome string. : No violent pressure, time approaching timeout. But it was a fluke.
#include <bits/stdc++.h>using namespace std; #define MIN (a) < (b) ( A):(B)) const int Maxn=20200;int pre[maxn*2],suf[maxn*2];int P[maxn*2];char str[maxn],trans[maxn*2];int Transform () {/ /memset (P,0,sizeof (p)); memset (pre,0,sizeof (pre)); memset (suf,0,sizeof (SUF)); int Len=strlen (str); Trans[0]= ' $ '; for (int i=1;i<=2*len;i+=2) {trans[i]= ' # '; TRANS[I+1]=STR[I/2]; } trans[2*len+1]= ' # '; trans[2*len+2]= ' @ '; return 2*len+1;} int Manacher () {int len=transform (); int mx=0,pos=0; for (int i=1;i<=len;i++) {if (i<mx) {p[i]=min (p[2*pos-i],mx-i); }else{p[i]=1; } for (; Trans[i+p[i]]==trans[i-p[i]];p [i]++); if (Mx<i+p[i]) {mx=i+p[i]; Pos=i; }} return len; int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%s", str); int Lens=strlen (str); if (lens<3) {printf ("no\n"); continue; }else if (lens==3) {printf ("yes\n"); continue; }else{int len= manacher (); for (int i=2;i<len;i++) {if (p[i]==i) {pre[i+p[i]-1]=1; } if (p[i]==len-i+1) {suf[i-p[i]+1]=1; }} int flag=0; for (int i=2;i<len&& (!flag), i++) {for (int j=1;j<p[i]&& (!flag); j + +) { if (Pre[i-j]&suf[i+j]) {flag=1; printf ("yes\n"); }}} if (!flag) {printf ("no\n"); }}} return 0;}
HDU 5340--three palindromes —————— "Manacher treatment palindrome string"