http://www.lydsy.com/JudgeOnline/problem.php?id=3676

过程很艰难了,第一次提交Manacher忘了更新p数组,超时,第二次是倍增的第0维直接在自动机里完成,但是忽略了增加新点时fa变动的情况,还是肉眼查错最管用。

得到的教训是既然倍增就在倍增的函数里完成,自动机就在自动机里完成,不要随便乱搞赋值。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=;
char ch[maxn]={};
char ch1[maxn*]={};
int siz,siz1;
struct sam{
int sig[];
int f,len,d[],num;
}t[maxn*];
int tot=,la=;
int loc[maxn]={},cnt[maxn*]={},b[maxn*]={},p[maxn*]={};
long long ans=;
void add(int z){
int x=++tot,i=la;
t[x].len=t[la].len+;
for(;i&&!t[i].sig[z];i=t[i].f)
t[i].sig[z]=x;
if(!i)t[x].f=;
else{
int p=t[i].sig[z];
if(t[p].len==t[i].len+)t[x].f=p;
else{
int y=++tot;
t[y]=t[p];t[y].len=t[i].len+;
t[p].f=t[x].f=y;
for(;i&&t[i].sig[z]==p;i=t[i].f)
t[i].sig[z]=y;
}
}
la=x;
}
void pre(){
int z;
for(int i=;i<=tot;i++)cnt[t[i].len]++;
for(int i=;i<=siz;i++)cnt[i]+=cnt[i-];
for(int i=tot;i;i--)b[cnt[t[i].len]--]=i;
for(int i=;i<=siz;i++)t[loc[i]].num=;
for(int i=tot;i;i--){
z=b[i];t[t[z].f].num+=t[z].num;
}
for(int i=;i<=tot;i++){
z=b[i];t[z].d[]=t[z].f;
for(int j=;j<=;j++)
t[z].d[j]=t[t[z].d[j-]].d[j-];
}
}
void getit(int l,int r){
r=r/;l=(l+)/;
if(r<l)return;
int z=loc[r];
for(int i=;i>-;i--){
if(t[t[z].d[i]].len>=r-l+){
z=t[z].d[i];
}
}
long long sum=(long long)t[z].num*(r-l+);
if(ans<sum)ans=sum;
}
void Manacher(){
for(int i=;i<=siz;i++)ch1[i*]=ch[i],ch1[i*-]='#';
ch1[siz*+]='#';ch1[siz*+]='\0';ch1[]='$';
siz1=siz*+;
int mx=,id=;
for(int i=;i<=siz1;i++){
if(mx>=i)p[i]=min(mx-i,p[id*-i]);
else p[i]=;
while(ch1[i+p[i]]==ch1[i-p[i]]){++p[i];getit(i-p[i]+,i+p[i]-);}
if(mx<i+p[i]){mx=i+p[i];id=i;}
}
}
int main(){
memset(t,,sizeof(t));
scanf("%s",ch+);siz=strlen(ch+);
for(int i=;i<=siz;i++){loc[i]=tot+;add(ch[i]-'a');}
pre();
Manacher();
printf("%lld\n",ans);
return ;
}

BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增的更多相关文章

  1. BZOJ 3676 [Apio2014]回文串 (后缀自动机+manacher/回文自动机)

    题目大意: 给你一个字符串,求其中回文子串的长度*出现次数的最大值 明明是PAM裸题我干嘛要用SAM做 回文子串有一个神奇的性质,一个字符串本质不同的回文子串个数是$O(n)$级别的 用$manach ...

  2. 【BZOJ 3676】 3676: [Apio2014]回文串 (SAM+Manacher+倍增)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2343  Solved: 1031 Description 考 ...

  3. [APIO2014]回文串 后缀自动机_Manancher_倍增

    Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstr ...

  4. bzoj 3676: [Apio2014]回文串 回文自动机

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 844  Solved: 331[Submit][Status] ...

  5. BZOJ 3676: [Apio2014]回文串

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2013  Solved: 863[Submit][Status ...

  6. 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串

    Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...

  7. bzoj 3676: [Apio2014]回文串【回文自动机】

    回文自动机板子 或者是SAM+manacher+倍增,就是manacher求本质不同回文串(让f++的串),然后在SAM倍增查询对应点出现次数 #include<iostream> #in ...

  8. ●BZOJ 3676 [Apio2014]回文串

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...

  9. BZOJ 3676 [Apio2014]回文串(回文树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题目大意] 考虑一个只包含小写拉丁字母的字符串s. 我们定义s的一个子串t的& ...

随机推荐

  1. js常见易错点

    原文:http://www.jianshu.com/p/1c77853d4f01 前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函 ...

  2. input只读属性readonly和disabled的区别

    主要区别: 参考: http://bbs.html5cn.org/forum.php?mod=viewthread&tid=84113&highlight=input http://b ...

  3. spring-boot-JdbcTemplate

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  4. pythonif语句和循环语句

    1.if语句用法 # if语句用法(缩进相同的成为一个代码块) score=90 if score>=60: print("合格") print("OK" ...

  5. 一个TCP报文段的数据部分最多为多少个字节,为什么

    IP数据报的最大长度=2^16-1=65535(字节)TCP报文段的数据部分=IP数据报的最大长度-IP数据报的首部-TCP报文段的首部=65535-20-20=65495(字节) 一个tcp报文段的 ...

  6. Linux内核中的Cache段

    Linux内核中的Cache段 原文地址:http://blogold.chinaunix.net/u2/85263/showart_1743693.html 最近移植LEON3的内核时,了解了一些简 ...

  7. 64_p3

    perl-Locale-Msgfmt-0.15-17.fc26.noarch.rpm 12-Feb-2017 03:11 20558 perl-Locale-PO-0.27-6.fc26.noarch ...

  8. MySQL Table Information

    show tables;                    --显示该数据库里的所有表show columns from 表名;         --显示表字段use information_sc ...

  9. pip安装模块时:error: command 'gcc' failed with exit status 1

    用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...

  10. django入门--django-blog-zinnia搭建个人博客

    1.安装python 选择合适python2.7及以上版本安装https://www.python.org/downloads/ 2.建立虚拟环境 这不是必须的,但是建议使用,为每个项目单独引入依赖, ...