题目链接:
http://poj.org/problem?id=3974
Palindrome
Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 13923 Accepted: 5340 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
Manacher算模板
https://blog.csdn.net/u013480600/article/details/23060715
直接上模板的
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
int read()//输入外挂
{
int ret=0, flag=0;
char ch;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
ret = ch - '0';
while((ch=getchar())>='0'&&ch<='9')
ret=ret*10+(ch-'0');
return flag ? -ret : ret;
}
const int MAXN=1000000+1000;
char S[MAXN*2];//改造后的串
char T[MAXN];//原始串
int p[MAXN*2];//储存长度
int Max;
void init()//改造原始串
{
int j=2;//要在外面初始化
S[0]='@',S[1]='#';
for(int i=0; T[i]; i++,j+=2)
{
S[j]=T[i];
S[j+1]='#';
}
S[j]='\0';
}
int manacher()
{
int id,mx=0;//mx指的是那个id能触及的最大位置 +1 的地方
Max=-INT_INF;
for(int i=1; S[i]; i++)
{
if(mx>i)
p[i]=min(p[2*id-i], mx-i);
else
p[i]=1;
while(S[i-p[i]] == S[i+p[i]])
p[i]++;
if(i+p[i]>mx)
{
mx=i+p[i];
id=i;
}
Max=max(Max,p[i]-1);
}
return Max;
}
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
int Case=1;
while(~scanf(" %s",T))
{
init();
if(T[0]=='E')
break;
printf("Case %d: ",Case++);
printf("%d\n",manacher());
}
return 0;
}