您的位置:首页 > 其它

POJ 3974 Palindrome 求最长回文子串 Manacher

2015-11-04 13:17 483 查看
纯模板题,用来练手,模板记错贡献一次wa。

Palindrome

Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 6206 Accepted: 2283
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
abcbabcbabcba
abacacbaaaab
END


Sample Output
Case 1: 13
Case 2: 6


Source

Seventh ACM Egyptian National Programming Contest
[Submit]   [Go
Back]   [Status]   [Discuss]


Home Page   

Go
Back  

To top

ACode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 1000005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
char str[maxn];
char ma[maxn<<1];
int mp[maxn<<1];
int cnt=1,ans;
void Manacher(){
int l=0;ma[l++]='$';ma[l++]='#';
int m=strlen(str);ans=-INF;
FOR(i,0,m-1){
ma[l++]=str[i];
ma[l++]='#';
}
ma[l]=0;
int mx=0,id=0;
FOR(i,0,l-1){
mp[i]=mx>i?min(mp[2*id-i],mx-i):1;
while(ma[i+mp[i]]==ma[i-mp[i]])mp[i]++;
if(mp[i]+i>mx){
mx=mp[i]+i;
id=i;
}
ans=max(ans,mp[i]);
}
printf("Case %d: %d\n",cnt++,ans-1);
}
int main(){
while(rds(str)&&strcmp(str,"END"))
Manacher();
return 0;
}
/*
abcbabcbabcba abacacbaaaab END
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: