您的位置:首页 > 其它

【天梯赛】L2-008. 最长对称子串(Manacher算法)

2018-03-21 13:39 351 查看
时间限制

100 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

陈越

对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定”Is PAT&TAP symmetric?”,最长对称子串为”s PAT&TAP s”,于是你应该输出11。

输入格式:

输入在一行中给出长度不超过1000的非空字符串。

输出格式:

在一行中输出最长对称子串的长度。

输入样例:

Is PAT&TAP symmetric?

输出样例:

11

分析:

见:http://blog.csdn.net/feng_zhiyu/article/details/79638825

#include <bits/stdc++.h>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define pb push_back
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=1e8+7;
const int N=2e3+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
const ll inf=0x3f3f3f3f3f3f3f3f;

int p
;
char s
,t
;
int init()
{
int len=strlen(s);
t[0]='$',t[1]='#';
int j=2;
for(int i=0; i<len; i++)
{
t[j++]=s[i];
t[j++]='#';
}
s[j]='\0';
return j;
}
void manacher()
{
int len=init();
int maxlen=0,mx=0,id;
for(int i=1; i<len; i++)
{
if(i<mx) p[i]=min(p[id*2-i],mx-i);
else p[i]=1;
while(t[i-p[i]]==t[i+p[i]]) p[i]++;
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
maxlen=max(maxlen,p[i]-1);
}
printf("%d\n",maxlen);
}
int main()
{
gets(s);
manacher();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT Manacher