poj 3974 Palindrome(最长回文子串,处理大数,Manacher算法)
来源:互联网 发布:新疆seo外包 编辑:程序博客网 时间:2024/05/17 05:11
1、http://poj.org/problem?id=3974
2、题目大意:
给定一串字符串,输出最长回文串的长度,此题难点在于字符串长度1000000,用二维dp会超时,在此用Manacher算法解决,见上一篇博客
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 2225 Accepted: 801
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
3、代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1000005char str[2*N+2];char st[N];int p[2*N+2];int main(){ int cas=0; while(scanf("%s",st)!=EOF) { if(strcmp(st,"END")==0) break; cas++; int nn=strlen(st); int n=2*nn+2; str[0]='$';//str首位置赋值$ for(int i=0;i<=nn;i++) { str[2*i+2]=st[i]; str[2*i+1]='#'; }//str从下标1开始 int ans=-999; memset(p,0,sizeof(p));//p[i]数组记录以str[i]为中心的最长回文子串的长度,p[i]-1即为原来字符串的长度 int mx=0;//mx用来记录p[i]中到达的最右端的值 int id;//id用来记录p[i]延长至最右端的下标i for(int i=1;i<n;i++) { if(mx>i) p[i]=min(p[2*id-i],mx-i);//不太明白,待深思。。。。 else p[i]=1; while(str[i-p[i]]==str[i+p[i]]) p[i]++; if(mx<i+p[i]) { mx=i+p[i]; id=i; } if(ans<p[i]) ans=p[i]; } printf("Case %d: %d\n",cas,ans-1);//输出的ans-1,即是最长的回文串长度 } return 0;}/*abcbabcbabcbaabacacbaaaabEND*/
- poj 3974 Palindrome(最长回文子串,处理大数,Manacher算法)
- poj 3974 Palindrome 【最长连续回文子串,Manacher算法】
- POJ 3974 Palindrome 最长回文子串(manacher算法)
- POJ 3974 Palindrome 求最长回文子串 Manacher
- POJ 3974 HDU 3068 最长回文子串 Manacher 算法
- 求解最长回文子串 Manacher算法 之 POJ 3974
- poj 3974 Manacher算法(判断最长回文子串)
- POJ 3974 Palindrome 最长回文子串
- POJ 3974 Palindrome【最长回文串(Manacher模板)】
- POJ-3974-Palindrome- Manacher 马拉车算法(On寻找最长回文串)
- manacher算法处理最长的回文子串(二)
- manacher算法处理最长的回文子串(一)
- 【字符串处理】最长回文子串笔记(Manacher算法)
- POJ 3974 Palindrome(最大回文串长度 Manacher算法)
- Manacher算法求最长回文子串
- Manacher算法求最长回文子串
- 最长回文子串(Manacher算法)
- Manacher算法 最长回文子串
- 使用jQuery实现隔行换色,批量绑定事件
- 豆皮 - STM32开发板入门教程(十七) - IAP 之串口篇 (原创)
- 内容过滤选择器
- Jquery的对象和dom方式对象的区别及转化
- 专家预测未来嵌入式人才的发展方向
- poj 3974 Palindrome(最长回文子串,处理大数,Manacher算法)
- 属性过滤选择器
- jQuery选择器分类,总结
- 经典的机器学习方面源代码库
- Mac下好用的HTTP/HTTPS抓包工具Charles
- Lambda表达式
- 关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi
- 如何在 Windows 8 設定開機啟動程式
- dijkstra算法