HDU 3068 longest input string marathon Template

Source: Internet
Author: User

I wrote this question once with a suffix array a few days ago. No doubt it's very touching.-_-|

 

Today, I happened to find the marathon template. O (n) will be done in time.

Reference: http://acm.uestc.edu.cn/bbs/read.php? Tid = 3258

 

 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define N 110010 6 char s[N*2],str[N*2]; 7 int n,p[N*2]; 8  9 void fun()10 {11     int len=(int)strlen(s);12     str[0]=‘$‘;str[1]=‘#‘;13     n=2;14     for(int i=0;i<len;i++)15     {16         str[n++]=s[i];17         str[n++]=‘#‘;18     }19     str[n]=‘\0‘;20 }21 22 void Manacher()23 {24     int i,id,mx=0;25     for(i=1;i<n;i++)26     {27         if(mx > i)28             p[i]=min(p[2*id-i],p[id]+id-i);29         else30             p[i]=1;31         for(;str[i+p[i]] == str[i-p[i]];p[i]++)32             ;33         if(p[i]+i > mx)34         {35             mx=p[i]+i;36             id=i;37         }38     }39 }40 41 void work()42 {43     int ans=-1;44     for(int i=1;i<n;i++)45         ans=max(ans,p[i]);46     printf("%d\n",ans-1);47 }48 49 int main()50 {51     while(scanf("%s",s)!=EOF)52     {53         fun();54         Manacher();55         work();56     }57     return 0;58 }

 

HDU 3068 longest input string marathon Template

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.