POJ3974 Palindrome(Manacher算法)
来源:互联网 发布:iphone照片整理软件 编辑:程序博客网 时间:2024/05/22 15:13
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 8552 Accepted: 3227
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.
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
也是一道模板题,不多说,上模板。
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;const int maxx=1000000+10;char c[maxx];char cnew[2*maxx];int p[2*maxx];int init(){int len=strlen(c);cnew[0]='$';cnew[1]='#';int j=2;for(int i=0;i<len;i++){ cnew[j++]=c[i]; cnew[j++]='#';}cnew[j]='\0';return j;}int Manacher(){int len=init();int maxlen=-1;int id;int mx=0;for(int i=1;i<len;i++){ if(i<mx)p[i]=min(p[2*id-i],mx-i); else p[i]=1; while(cnew[i-p[i]]==cnew[i+p[i]])p[i]++; if(mx<i+p[i]) { id=i; mx=i+p[i]; }maxlen=max(maxlen,p[i]-1);}return maxlen;}int icase=1;int main(){while(~scanf("%s",c)){ if(strcmp(c,"END")==0)break; printf("Case %d: %d\n",icase++,Manacher());}return 0;}
0 0
- POJ3974 Palindrome(Manacher算法)
- POJ3974 Palindrome (manacher算法)
- Palindrome(poj3974)(manacher算法)
- poj3974 Palindrome(manacher)
- poj3974:Palindrome(manacher模板)
- hdu3068 最长回文 poj3974 Palindrome(Manacher算法)
- 【POJ3974】【Palindrome】【Manacher裸题】
- POJ3974 Palindrome(Manacher)
- [学习][poj3974]manacher Palindrome
- poj3974 Palindrome(manacher)
- poj3974 Palindrome,回文串,Manacher
- poj3974之manacher算法
- poj3974(Manacher算法)
- poj3974(manacher)
- 【POJ3974】Palindrome Manacher、模板题 裸题
- POJ3974 Palindrome 解题报告【字符串】【Manacher】
- POJ3974-Manacher算法--O(n)回文子串算法
- poj3974(最长回文串)(Manacher算法)
- 【蓝桥杯单片机组】(6)uart
- AJAX运行过程中5种状态
- 笔试题目
- 后缀自动机+DP NOI2015 Day 2 品酒大会
- 音频变调算法总结
- POJ3974 Palindrome(Manacher算法)
- 看雪学院-浅入浅出Android安全 笔记
- Mac OS系统修改Hosts文件的方法
- 完全基于Html5的Web Dicom Viewer
- Android中Fragment中onActivityResult不被调用的解决方案
- 单向链表的各个操作
- C++学习-继承和派生
- SX1278传输距离测试
- Linux下的UDP Socket简单例子