题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3068
题意
最长回文子串
思路
马拉车模板题
代码
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<PII,int> PPI;
typedef pair<ll,int> PLI;
const double PI=acos(-1);
const int maxn = 1e5+1e4+10;
const int maxm = 1e6 + 10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
using namespace std;
char s[maxn],st[maxn*2];
int p[maxn*2];
void init()
{
int len=strlen(s);
st[0]='$',st[1]='#';
int j=2;
for(int i=0;i<len;i++)
{
st[j++]=s[i];
st[j++]='#';
}
st[j]='\0';
}
int manacher()
{
init();
int len=2*strlen(s)+2;
int maxlen=-1;
int id,mx=0;
for(int i=1;i<len;i++)
{
if(i<mx) p[i]=min(p[2*id-i],mx-i);
else p[i]=1;
while(st[i-p[i]]==st[i+p[i]]) p[i]++;
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
maxlen=max(maxlen,p[i]-1);
}
return maxlen;
}
int main()
{
while(~scanf("%s",s))
{
printf("%d\n",manacher());
}
}