HDU 5340-three palindromes (manacher algorithm)

Source: Internet
Author: User

Title Address: HDU 5340
Test instructions: Ask if the string str can be decomposed into three non-empty palindrome strings.
Idea: We can get two arrays first and last by processing the string according to the Manacher algorithm and the p array produced during processing.
First stores the value of the radius possible for a palindrome string.
Last store is the radius possible value of the third palindrome string.
We can enumerate the first palindrome string and the third palindrome string, and then find the initial position and end position of the second palindrome according to the radius. Then we calculate the midpoint of the second palindrome string:
1. If LL>RR, the second string is a negative length, pass
2. If p[mid]=1, the second palindrome string is "#", equivalent to NULL, pass
3. If the length of the three substrings is greater than Len, the length of the match is proved to be the same.

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn=110010;CharSTR[MAXN];Chars[maxn*2];intp[maxn*2];intLenintfirst[maxn*2];intlast[maxn*2];voidManacher () {intI s[0]=' $ '; s[1]=' # '; for(i=0; i<len;i++) {s[i*2+2]=str[i]; s[i*2+3]=' # '; } s[i*2+2]=' + '; len=2*i+2;intMaxl,id; Id=maxl=0;memset(P,0,sizeof(p)); for(intI=0; i<len; i++) {if(p[id]+id>i) P[i]=min (p[2*ID-I],P[ID]+ID-I);Elsep[i]=1; while(S[i+p[i]]==s[i-p[i]]) p[i]++;if(P[i]+i>p[id]+id)        {id=i; }if(P[I]&GT;MAXL)    Maxl=p[i]; }}intMain () {intTscanf("%d", &t); while(t--) {scanf('%s ', &AMP;STR); len=strlen(str); Manacher ();intl1=0, r1=0; for(intI=2; i<=len-2; i++) {if(I==p[i]) first[l1++]=i;if(p[i]==len-i) last[r1++]=i; }intflag=0;intLl,rr,mid; for(intI=0; i<l1;i++) { for(intj=0; j<r1;j++) {ll=first[i]+p[first[i]]; RR=LAST[J]-P[LAST[J]];if(LL&GT;RR)Continue; Mid= (LL+RR)/2;if(ll==rr&&s[mid]==' # ')Continue;if(p[first[i]]*2-1+p[mid]*2-1+p[last[j]]*2-1>=len+1) {flag=1; Break; }            }if(flag) Break; }if(flag)puts("Yes");Else            puts("No"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.