HDU-5340 three palindromes (Manacher algorithm)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5340

Test Instructions

Determine if the string s can be divided into three non-empty palindrome strings

Analysis

Manacher preprocessing the position of prefix and suffix palindrome, enumerate the first palindrome string and the third palindrome string, so get the second palindrome string interval, find the midpoint, because manacher processing all the palindrome string length is odd, and then according to the palindrome radius of the midpoint to determine whether the middle part is a palindrome, Complexity O (n2). As for the N2 complexity why can water past. I don't understand.

#include <iostream>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<cstdio>#include<algorithm>#include<map>#include<Set>#defineRep (i,e) for (int i=0;i< (e); i++)#defineREP1 (i,e) for (int i=1;i<= (e); i++)#defineREPX (i,x,e) for (int i= (x); i<= (e); i++)#defineX First#defineY Second#definePB push_back#defineMP Make_pair#defineMset (Var,val) memset (Var,val,sizeof (VAR))#defineSCD (a) scanf ("%d", &a)#defineSCDD (A, b) scanf ("%d%d", &a,&b)#defineSCDDD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#definePD (a) printf ("%d\n", a)#defineSCL (a) scanf ("%lld", &a)#defineSCLL (A, b) scanf ("%lld%lld", &a,&b)#defineSclll (a,b,c) scanf ("%lld%lld%lld", &a,&b,&c)#defineIOS Ios::sync_with_stdio (false); Cin.tie (0)using namespaceStd;typedefLong Longll;template<classT>voidTest (T a) {cout<<a<<Endl;} Template<classTclassT2>voidTest (T a,t2 b) {cout<<a<<" "<<b<<Endl;} Template<classTclassT2,classT3>voidTest (T a,t2 b,t3 c) {cout<<a<<" "<<b<<" "<<c<<Endl;}Const intN = 1e6+Ten;//const int MAXN =;Const intINF =0x3f3f3f3f;Constll INF =0x3f3f3f3f3f3f3f3fll;Constll mod =200907;intT;voidtestcase () {printf ("Case #%d:",++T);}Const intMAXN = 4e4+5;Const intMAXM = -;CharMA[MAXN],S[MAXN];intMP[MAXN];intPRE[MAXN],SUF[MAXN];voidManacher (intLen) {    intL=0; Ma[l++]='$'; Ma[l++]='#';  for(intI=0; i<len;i++) ma[l++]=s[i],ma[l++]='#'; MA[L]=0; intmx=0, id=0;  for(intI=0; i<l;i++) {Mp[i]=mx>i?min (mp[2*id-i],mx-i):1;  while(Ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++; if(i+mp[i]>MX) {MX=i+Mp[i]; ID=i; }    }}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL    intT;    SCD (t);  while(t--) {scanf ("%s", s); intLen =strlen (s);        Manacher (len); intL=0, r=0; Len=2*len+1;  for(intI=1; i<=len;i++){            if(mp[i]==i&&i!=1) pre[l++]=Mp[i]; //mp[i]==i Guarantee Mp[i] can be the radius of the first palindrome, join the pre array, I! =1 guarantee that the first palindrome is not empty            if(mp[i]+i-1==len&&i!=len) suf[r++]=Mp[i]; }        inti,j;  for(i=l-1; i>=0; i--){             for(j=0; j<r;j++){                intt1=2*Pre[i]; intt2=len+1-2*Suf[j]; intTMP = (T1+T2) >>1; if(T1&GT;T2)Continue; if(mp[tmp]==1)Continue; if(mp[tmp]*2-1>=t2-t1+1) Break; }            if(J<r) Break; }        if(i>=0) puts ("Yes"); ElsePuts"No"); }    return 0;} 

HDU-5340 three palindromes (Manacher algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.