题目链接:双倍回文

  回文自动机第二题。构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(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. 【office2010】office2010安装问题的解决方案。

    今天想在公司电脑上按上一个office2010,结果出现一个问题,导致研究了一下午才解决:现总结解决方案: 安装office 2010,提示需要安装MSXML版本6.10.1129.0组件.但是在网上 ...

  2. 百度地图api添加自定义控件

    官网栗子:http://lbsyun.baidu.com/jsdemo.htm#b0_6 <!DOCTYPE html><html><head> <meta ...

  3. mybatis的<choose>和<when>、<otherwise>标签

    SELECT<choose> <when test='timeType=="yy"'> TO_CHAR(REPORT_TIME,'yyyy') </w ...

  4. 在github上参与开源项目日常流程

    转载自:http://blog.csdn.net/five3/article/details/9307041 1. 注册帐号 打开https://github.com/,填写注册信息并提交. 2. 登 ...

  5. 表格table列宽度控制<colgroup>

    <colgroup> 标签用于对表格中的列进行组合,以便对其进行格式化. 通过使用 <colgroup> 标签,可以向整个列应用样式,而不需要重复为每个单元格或每一行设置样式. ...

  6. 转载:SQL Server编程基本语法

    一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 ) select @user1='张三' print @user1 ) print @user ...

  7. MVC学习之HtmlHelper

    1.为什么要使用HtmlHelper? 1.首先HtmlHelper是一个类型,MVC中的ViewPage<TModel>中的一个属性Html属性,这个属性的类型就是HtmlHelper& ...

  8. java 获取当前进程id 线程id

    java  获取当前进程id  线程id RuntimeMXBean (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/j ...

  9. Python网络爬虫学习总结

    1.检查robots.txt 让爬虫了解爬取该网站时存在哪些限制. 最小化爬虫被封禁的可能,而且还能发现和网站结构相关的线索. 2.检查网站地图(robots.txt文件中发现的Sitemap文件) ...

  10. java 并查集

    并查集代码 并查集优化⼀ 并查集优化⼆ 实战题⽬目1. https://leetcode.com/problems/number-of-islands/2. https://leetcode.com/ ...