最长回文子串
来源:互联网 发布:r语言 java 编辑:程序博客网 时间:2024/05/29 19:37
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 5123 Accepted: 1836
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 <iostream>#include <algorithm>#include <string.h>#include <math.h>using namespace std;const int Maxn=1000010;char Ma[Maxn*2];int Mp[Maxn*2];void Manacher(char s[],int len){int l=0;Ma[l++]='$';Ma[l++]='#';for (int i=0;i<len;i++){Ma[l++]=s[i];Ma[l++]='#';}//cout<<"1"<<endl;Ma[l]=0;int mx=0,id=0;for(int i=0;i<l;i++){Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;while(Ma[i+Mp[i]]==Ma[i-Mp[i]])Mp[i]++;if(i+Mp[i]>mx){mx=i+Mp[i];id=i;}}}char s[Maxn];int main(){//freopen("test3974.in","r",stdin);int t=1;while(1){ scanf("%s",s);int len=strlen(s);if(s[0]=='E'&&s[1]=='N'&&s[2]=='D'&&len==3)break;Manacher(s,len);int ans=0;for(int i=0;i<2*len+2;i++) { ans=max(ans,Mp[i]-1); } printf("Case %d: %d\n",t++,ans );}}
0 0
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- 最长回文子串
- d3.js画一个直方图
- Http Components 使用相关资源
- Hibernate概览
- Android SDK 在线更新镜像服务器资源
- iOS UISlider
- 最长回文子串
- C# 处理JSON
- 尚振宏等《二值图像中拐点的实时检测算法》matlab实现
- ubuntu 有时使用man xxx查询函数时查不到的解决方法
- 指针与数组
- POJ 题目1753 Flip Game(DFS)
- Android学习 - 小知识(sqlite与ViewPager和ViewFilpper简介)
- Eclipse安装git插件
- spring对WebSocket支持