HDU 5340-Three Palindromes (Manacher algorithm)

Source: Internet
Author: User

HDU 5340-Three Palindromes (Manacher algorithm)

 
Question: Can str be broken down into three non-empty return strings.
Train of Thought: We process strings according to the Manacher algorithm. We can obtain two arrays first and last, which are P arrays generated during processing.
First stores the possible radius values of the first input string.
Last stores the possible radius value of the third input string.
Based on first and last, we can enumerate the first and third input strings, and then find the initial and end positions of the second input string based on the radius. Then calculate the midpoint of the Second input string:
1. If ll> rr, the length of the second string is negative, pass
2. If p [mid] = 1, the second input string is "#", which is equivalent to null. pass
3. If the length of the three sub-strings is greater than len, it indicates that the length is correct. Just jump out.

#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include #include 
        
         #include 
         
          #include 
          
           #include
           #pragma comment(linker, /STACK:102400000,102400000)using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=110010;char str[maxn];char s[maxn*2];int p[maxn*2];int len;int first[maxn*2];int last[maxn*2];void Manacher(){ int i; s[0]='$'; s[1]='#'; for(i=0;i
            
             i) p[i]=min(p[2*id-i],p[id]+id-i); else p[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]>MaxL) MaxL=p[i]; }}int main(){ int T; scanf(%d,&T); while(T--){ scanf(%s,&str); len=strlen(str); Manacher(); int l1=0,r1=0; for(int i=2;i<=len-2;i++){ if(i==p[i]) first[l1++]=i; if(p[i]==len-i) last[r1++]=i; } int flag=0; int ll,rr,mid; for(int i=0;i
             
              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;}
             
            
          
         
        
       
      
     
    
   
  

 

Related Article

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.