BZOJ2342:双倍回文(回文自动机+树上差分)
2018-03-05 19:20
363 查看
题面
题意:给你一个串,求出它的最长子串
满足该子串有两个长度相等且为偶数的回文串拼接而成
该串显然是回文串,就对应回文自动机上的一个状态
若某个状态的长度为4的倍数
且存在某个祖先的长度为其一半
则该状态可以贡献答案
因为祖先即回文后缀和前缀
长度为其一半的前缀和后缀都是回文串
显然符合题意
我的做法是树上差分
题意:给你一个串,求出它的最长子串
满足该子串有两个长度相等且为偶数的回文串拼接而成
该串显然是回文串,就对应回文自动机上的一个状态
若某个状态的长度为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; }
相关文章推荐
- [回文自动机]bzoj2342: [Shoi2011]双倍回文
- bzoj 2342: 双倍回文 回文自动机
- [BZOJ2342] [Shoi2011]双倍回文(manacher)
- BZOJ2342: [Shoi2011]双倍回文
- BZOJ2342 [Shoi2011]双倍回文 【manacher】
- bzoj 2342: [Shoi2011]双倍回文
- bzoj2342 [Shoi2011]双倍回文
- bzoj2342 [Shoi2011] 双倍回文
- BZOJ2342 [Shoi2011]双倍回文 【manacher】
- BZOJ 2342 [Shoi2011]双倍回文(manacher+并查集)
- bzoj2342【SHOI2011】双倍回文
- BZOJ 2342: 【SHOI2011】 双倍回文
- [BZOJ2342][Shoi2011]双倍回文(manacher)
- [BZOJ2342][SHOI2011]双倍回文
- 【BZOJ2342】双倍回文(回文树)
- bzoj 2342 [Shoi2011]双倍回文
- BZOJ 2342 [Shoi2011] 双倍回文 Manacher + set维护
- 【BZOJ2342】【Shoi2011】双倍回文 Manacher算法
- 【BZOJ2342】【Shoi2011】双倍回文 Manacher+枚举+最优性剪枝
- 【BZOJ2342】双倍回文(回文树)