隐藏页面特效

POJ 3974 Palindrome

D - Palindrome
Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

abcbabcbabcba abacacbaaaab END

Sample Output

Case 1: 13 Case 2: 6

求最长回文串的长度。

(manacher算法)

#include<cstdio> #include<cstring> #include<iostream> #define m(s) memset(s,0,sizeof s); using namespace std; const int N=1e6+5; int l,cas,len,p[N<<1]; char s[N],S[N<<1]; void manacher(){ int ans=0,id=0,mx=-1; for(int i=1;i<l;i++){ if(id+mx>i) p[i]=min(p[id*2-i],id+mx-i); while(i-p[i]-1>=0&&i+p[i]+1<=l&&S[i-p[i]-1]==S[i+p[i]+1]) p[i]++; if(id+mx<i+p[i]) id=i,mx=p[i]; ans=max(ans,p[i]); } printf("Case %d: %d\n",++cas,ans); } int main(){ while(scanf("%s",s)==1){ if(s[0]=='E') break; len=strlen(s);m(p);m(S); l=-1; for(int i=0;i<len;i++) S[++l]='#',S[++l]=s[i]; S[++l]='#'; manacher(); } return 0; }

//====================================================

//hash有点慢 #include<cstdio> #include<cstring> #include<iostream> using namespace std; typedef int i64; const int N=1e6+5; int n,m,cas,ans,a[N<<1];char s[N]; i64 P,pow[N<<1],hash_l[N<<1],hash_r[N<<1]; void get_hash(){ pow[0]=1;hash_r[0]=hash_l[m+1]=0; for(int i=1;i<=m;i++) pow[i]=pow[i-1]*P; for(int i=1;i<=m;i++) hash_r[i]=hash_r[i-1]*P+a[i]; for(int i=m;i>=1;i--) hash_l[i]=hash_l[i+1]*P+a[i]; } int main(){ P=29; while(scanf("%s",s+1)==1){ if(s[1]=='E') break; n=strlen(s+1);ans=m=0; for(int i=1;i<=n;i++){ a[++m]='#'; a[++m]=s[i]-'a'; } a[++m]='#'; get_hash(); int l,r,mid; for(int i=1;i<=m;i++){ l=0; if(i-1<m-i) r=i; else r=m-i+1; while(r-l>1){ mid=l+r>>1; i64 hash_to_l=hash_r[i-1]-hash_r[i-mid-1]*pow[mid]; i64 hash_to_r=hash_l[i+1]-hash_l[i+mid+1]*pow[mid]; if(hash_to_l==hash_to_r) l=mid; else r=mid; } ans=max(ans,l); } printf("Case %d: %d\n",++cas,ans); } return 0; }

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/5459916.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(303)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示