BZOJ 2342: 【SHOI2011】 双倍回文
题目链接:双倍回文
回文自动机第二题。构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(u\)顺着\(fail\)指针往上跳,可以找到一个节点\(x\)满足\(2len_x=len_u\)。当然还需要\(len_u\)是\(4\)的倍数。
然后我们把\(fail\)树构出来,在上面\(dfs\)一遍就做完了。
下面贴代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 500010 using namespace std;
typedef long long llg; char a[maxn];
int l[maxn],s[maxn][26],f[maxn];
int head[maxn],next[maxn],to[maxn];
int tt,la,ans,dt;
bool w[maxn]; void add(int c,int n){
int p=la;
while(a[n-l[p]-1]!=a[n]) p=f[p];
if(!s[p][c]){
int np=++tt,k=f[p]; l[np]=l[p]+2;
while(a[n-l[k]-1]!=a[n]) k=f[k];
f[np]=s[k][c]; s[p][c]=np;
}
la=s[p][c];
} void link(int x,int y){to[++dt]=y;next[dt]=head[x];head[x]=dt;}
void dfs(int u){
if(~l[u]&1) w[l[u]]=1;
if(l[u]%4==0 && w[l[u]>>1]) ans=max(ans,l[u]);
for(int i=head[u];i;i=next[i]) dfs(to[i]);
w[l[u]]=0;
} int main(){
File("a");
l[++tt]=-1; f[0]=1;
int n;scanf("%d %s",&n,a+1);
for(int i=1;i<=n;i++) add(a[i]-'a',i);
for(int i=tt;i>1;i--) link(f[i],i);
dfs(1); dfs(0); printf("%d",ans);
return 0;
}
BZOJ 2342: 【SHOI2011】 双倍回文的更多相关文章
- 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...
- BZOJ 2342: [Shoi2011]双倍回文 马拉车算法/并查集
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1123 Solved: 408 题目连接 http://w ...
- bzoj 2342: [Shoi2011]双倍回文 -- manacher
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符 ...
- Manacher || BZOJ 2342: [Shoi2011]双倍回文 || Luogu P4287 [SHOI2011]双倍回文
题面:[SHOI2011]双倍回文 题解:具体实现时,就是在更新mr时维护前半段是回文串的最长回文串就好了 正确性的话,因为到i时如果i+RL[i]-1<=mr,那么答案肯定在i之前就维护过了: ...
- BZOJ 2342 [Shoi2011]双倍回文(manacher+并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题目大意] 记Wr为W串的倒置,求最长的形如WWrWWr的串的长度. [题解] ...
- BZOJ 2342 [SHOI2011]双倍回文 (回文自动机)
题目大意:略 先建出$PAM$ 因为双倍回文串一定是4的倍数,所以找出$PAM$里所有$dep$能整除4的节点 看这个串是否存在一个回文后缀,长度恰好为它的一半,沿着$pre$链往上跳就行了 暴跳可能 ...
- BZOJ 2342 [Shoi2011]双倍回文(Manacher)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2342 题意:求最长子串使得它有四个相同的回文串SSSS相连组成. 首先跑一边Manach ...
- BZOJ 2342: [Shoi2011]双倍回文 [Manacher + set]
题意: 求最长子串使得它有四个相同的回文串SSSS相连组成 枚举中间x 找右边的中间y满足 y-r[y]<=x y<=x+r[x]/2 用个set维护 注意中间只能是# #include ...
- bzoj 2342 [Shoi2011]双倍回文(manacher,set)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题意] 求出形如w wR w wR的最长连续子串. [思路] 用manache ...
- BZOJ 2342: [Shoi2011]双倍回文
Sol Manacher. 非常裸的Manacher啊...为什么有那么多人写Manacher+并查集?Set?Treap?...好神奇... 你只需要在 \(p[i]++\) 的位置加上判断就可以了 ...
随机推荐
- .net asp iis服务器如何让外部访问自己的网站
1.控制面板-防火墙-高级设置-入站规则-右侧的BranchCache内容检索,右键启用规则.
- Java.lang的研究(分析包含的重要类和接口)
Java.lang包是Java中使用最广泛的一个包,它包含很多定义的类和接口. java.lang包包括以下这些类: Boolean Byte Character Class ClassLoader ...
- 310实验室OTL问题
1.首先是下载OTL文件包,网址是:github.com/O-T-L,可以直接点击网页上的下载链接,也可以使用unlix命令,git clone https://github.com/O-T-L/OT ...
- shell 输出文件内容
cat $filepath | while read line; do echo $line ; done #!/bin/bash #filepath=/opt/jenkins_home/worksp ...
- ZOJ 3212 K-Nice
K-Nice Time Limit: 1 Second Memory Limit: 32768 KB Special Judge This is a super simple pr ...
- 焦作网络赛L-Poor God Water【矩阵快速幂】
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- Google Now 'not available in your country'
Google Now 'not available in your country' Don't know how to cope with this problem.
- 如何在window的location使用target
在页面中window的location跳转时,指定页面在框架中跳转 1. 如果你要让最顶层的框架跳转,就是整个页面,相当于用traget指向顶层 window.top.location = & ...
- 双态运维联盟(BOA)正式成立
3月1日,由联想.新华三.华为等12家IT企业在北京正式达成协议,联合发起成立“双态运维联盟”.中国电子工业标准技术协会.信息技术服务分会数据中心运营管理工作组(DCMG)组长肖建一先生出席了会议. ...
- 安装Ruby、Sass在WebStrom添加Watcher实现编辑scss文件时自动生成.map和压缩后的.css文件
前言 这段时间一直在看Bootstrap,V3官方直接提供了Less版本的源码,就先将Less学完了,很简单的语法,学习写Demo都是在Webstorm里写的,配置了Watcher自动编译(详见< ...