poj Palindrome 3974 (字符串&manacher)
来源:互联网 发布:软件问题跟踪表 编辑:程序博客网 时间:2024/05/01 12:17
POJ - 3974
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.
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<stdio.h>#include<string.h>#define N 1000010#include<algorithm>using namespace std;char s[N<<1];char a[N];int p[N<<1];int manacher(){int l=strlen(s),i;int r=1,m=1;int ans=0;memset(p,0,sizeof(p));p[1]=p[0]=1;for(i=2;i<l;i++){if(r>i)p[i]=min(p[m*2-i],r-i);elsep[i]=1;while(s[i-p[i]]==s[i+p[i]])p[i]++;if(p[i]+i>r){r=p[i]+i;m=i;}ans=max(ans,p[i]);}return ans-1;}int main(){int T=1;int i,j;while(scanf("%s",a)&&strcmp(a,"END")){int l=strlen(a);s[0]='@';for(i=0;i<l;i++){s[i*2+1]='#';s[i*2+2]=a[i];}s[i*2+1]='#';s[i*2+2]='\0';int ans=manacher();printf("Case %d: %d\n",T++,ans);}return 0;}
0 0
- poj Palindrome 3974 (字符串&manacher)
- POJ 3974 Palindrome(manacher)
- POJ 3974 Palindrome(Manacher)
- POJ 3974 Palindrome(Manacher)
- 【Manacher】 POJ 3974 Palindrome
- POJ 3974 Palindrome Manacher
- POJ 3974 Palindrome【Manacher】
- poj 3974 Palindrome(manacher)
- POJ 3974 - Palindrome(manacher)
- POJ - 3974 Palindrome(manacher)
- POJ 3974 Palindrome manacher
- POJ 3974 Palindrome Manacher
- POJ-3974 Palindrome(manacher算法)
- POJ:3974 Palindrome (Manacher算法)
- 【manacher算法】POJ 3974 Palindrome
- POJ 3974 Palindrome (Manacher算法)
- POJ 3974-Palindrome(Manacher算法)
- POJ 3974 Palindrome 最长回文子串(manacher算法)
- 屏幕空间实时非平面反射
- 第6章 传输层
- 网上商城404错误解决
- 数字图像处理:第二十二章 基于模型的编码
- sqlldr默认字符串最大长度修改和ctl文件的格式
- poj Palindrome 3974 (字符串&manacher)
- 寒霜引擎(镜之边缘:催化剂)中的屏幕空间反射
- FMDB简单介绍
- android addIdleHandler 空闲线程 解析
- 数字图像处理:第二十三章 基于内容的图象与视频检索
- 1046. Shortest Distance (20)
- 计算机如何表示浮点数(小数)
- jade模板引擎(一)
- ASP.NET login控件