博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3974 Palindrome
阅读量:6568 次
发布时间:2019-06-24

本文共 3289 字,大约阅读时间需要 10 分钟。

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

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

abcbabcbabcbaabacacbaaaabEND

Sample Output

Case 1: 13Case 2: 6

求最长回文串的长度。

(manacher算法)

#include
#include
#include
#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
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

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

//hash有点慢#include
#include
#include
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
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;}

 

 

 

转载于:https://www.cnblogs.com/shenben/p/5459916.html

你可能感兴趣的文章
前端面试通关指南
查看>>
制作首页的显示列表。
查看>>
同样加班 不同收获
查看>>
数据公钥加密和认证中的私钥公钥
查看>>
c语言中的位移位操作
查看>>
object-c语言的nonatomic,assign,copy,retain的区别
查看>>
js 正则之检测素数
查看>>
linux-多线程
查看>>
第40周二
查看>>
使用虚拟机运行Ubuntu时,主机与宿主机共享文件的方法。
查看>>
EJB究竟是什么,真的那么神奇吗??
查看>>
海茶3 らぶデス3 入门经典教程
查看>>
pstree命令
查看>>
css选择器顺序的小技巧
查看>>
dojo 学习笔记之dojo.query - query(id) 与query(class)的差别
查看>>
Java基础加强总结(三)——代理(Proxy)
查看>>
一步一步写算法(之hash表)
查看>>
C99规范
查看>>
BZOJ3799 : 字符串重组
查看>>
数据持久化的复习
查看>>