题目链接:双倍回文

  回文自动机第二题。构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(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】 双倍回文的更多相关文章

  1. 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...

  2. BZOJ 2342: [Shoi2011]双倍回文 马拉车算法/并查集

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1123  Solved: 408 题目连接 http://w ...

  3. bzoj 2342: [Shoi2011]双倍回文 -- manacher

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符 ...

  4. Manacher || BZOJ 2342: [Shoi2011]双倍回文 || Luogu P4287 [SHOI2011]双倍回文

    题面:[SHOI2011]双倍回文 题解:具体实现时,就是在更新mr时维护前半段是回文串的最长回文串就好了 正确性的话,因为到i时如果i+RL[i]-1<=mr,那么答案肯定在i之前就维护过了: ...

  5. BZOJ 2342 [Shoi2011]双倍回文(manacher+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题目大意] 记Wr为W串的倒置,求最长的形如WWrWWr的串的长度. [题解] ...

  6. BZOJ 2342 [SHOI2011]双倍回文 (回文自动机)

    题目大意:略 先建出$PAM$ 因为双倍回文串一定是4的倍数,所以找出$PAM$里所有$dep$能整除4的节点 看这个串是否存在一个回文后缀,长度恰好为它的一半,沿着$pre$链往上跳就行了 暴跳可能 ...

  7. BZOJ 2342 [Shoi2011]双倍回文(Manacher)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2342 题意:求最长子串使得它有四个相同的回文串SSSS相连组成. 首先跑一边Manach ...

  8. BZOJ 2342: [Shoi2011]双倍回文 [Manacher + set]

    题意: 求最长子串使得它有四个相同的回文串SSSS相连组成 枚举中间x 找右边的中间y满足 y-r[y]<=x y<=x+r[x]/2 用个set维护 注意中间只能是# #include ...

  9. bzoj 2342 [Shoi2011]双倍回文(manacher,set)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题意] 求出形如w wR w wR的最长连续子串. [思路] 用manache ...

  10. BZOJ 2342: [Shoi2011]双倍回文

    Sol Manacher. 非常裸的Manacher啊...为什么有那么多人写Manacher+并查集?Set?Treap?...好神奇... 你只需要在 \(p[i]++\) 的位置加上判断就可以了 ...

随机推荐

  1. Eclipse常用快捷键 及 不格式化注释

    eclipse不格式化注释 - [自写] 2007-08-15   刚才在Eclipse3.2上写东西,我写好的注释,整整齐齐的,我一格式化代码,就变得七七八八的了.   试着在Perferences ...

  2. Android EditText光标颜色 与inputType

    1.EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色的   android:textCursorDrawable="@null&q ...

  3. CentOS6.8系统安装Oracle11g

    1.官网上下载 软件安装包: linux.x64_11gR2_database_1of2.zip linux.x64_11gR2_database_2of2.zip 解压后: 生成文件夹: datab ...

  4. OC开发_Storyboard——多线程、UIScrollView

    一.多线程 1.主队列:处理多点触控和所有UI操作(不能阻塞.主要同步更新UI) dispatch_queue_t mainQueue = dispatchg_get_main_queue(); // ...

  5. Java 泛型 <? super T> 中 super 怎么 理解?与 < ? extends T>有何不同?

    Java 泛型 <? super T> 中 super 怎么 理解?与 extends 有何不同? 简介 前两篇文章介绍了泛型的基本用法.类型擦除以及泛型数组.在泛型的使用中,还有个重要的 ...

  6. 170621、springboot编程之全局异常捕获

    1.创建GlobalExceptionHandler.java,在类上注解@ControllerAdvice,在方法上注解@ExceptionHandler(value = Exception.cla ...

  7. chinese-typesetting:更好的中文文案排版

    欢迎指正.GitHub 地址:https://github.com/jxlwqq/chinese-typesetting 更好的中文文案排版 统一中文文案.排版的相关用法,降低团队成员之间的沟通成本, ...

  8. HDU 6016 Count the Sheep

    Count the Sheep Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. acceptorThreadCount

    Apache Tomcat 7 Configuration Reference (7.0.92) - The HTTP Connector https://tomcat.apache.org/tomc ...

  10. mysql 数据操作 多表查询 子查询 带比较运算符的子查询

    带比较运算符的子查询 #比较运算符:=.!=.>.>=.<.<=.<> #查询大于所有人平均年龄的员工名与年龄 思路 先拿到所有人的平均年龄然后 再用另外一条sql ...