您的位置:首页 > 其它

BZOJ2342:双倍回文(回文自动机+树上差分)

2018-03-05 19:20 363 查看
题面

题意:给你一个串,求出它的最长子串

满足该子串有两个长度相等且为偶数的回文串拼接而成

该串显然是回文串,就对应回文自动机上的一个状态

若某个状态的长度为4的倍数

且存在某个祖先的长度为其一半

则该状态可以贡献答案

因为祖先即回文后缀和前缀

长度为其一半的前缀和后缀都是回文串

显然符合题意

我的做法是树上差分

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))

typedef long long LL;

const int N=500500;

int n,ans;
int son
[26],pre
,len
,cnt,last;
int to
,nex
,head
,cur;
bool ok
;
char s
;

void add(int u,int v)
{
to[++cur]=v;
nex[cur]=head[u];
head[u]=cur;
}

void dfs(int x)
{
if(ok[len[x]])
ans=max(ans,len[x]);
if(len[x]%2==0)
ok[len[x]<<1]=1;
for(int h=head[x];h;h=nex[h])
dfs(to[h]);
if(len[x]%2==0)
ok[len[x]<<1]=0;
}

void Insert(int pos)
{
int x=last,ch=s[pos]-'a';
while(s[pos-len[x]-1]!=s[pos])
x=pre[x];
if(!son[x][ch])
{
a88d

len[++cnt]=len[x]+2;
int y=pre[x];
while(s[pos-len[y]-1]!=s[pos])
y=pre[y];
pre[cnt]=son[y][ch];
son[x][ch]=cnt;
}
last=son[x][ch];
}

int main()
{
pre[0]=pre[1]=cnt=1;
len[1]=-1;

cin>>n;
scanf("%s",s+1);
for(int i=1;i<=n;i++)
Insert(i);

add(1,0);
for(int i=2;i<=cnt;i++)
add(pre[i],i);

dfs(1);
cout<<ans<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: