题面

初见SAM

洛谷数据太弱了,我SAM写错了居然有90pts=。=???

SAM求一个子串$(l,r)$的出现次数:从右端点对应状态开始在parent树上倍增,当目标节点的$len$大于等于子串长度时就往上跳,最后所在节点的$len$就是该串的出现次数

于是边$manacher$边在SAM上统计当前串的出现次数即可,复杂度$O(n\log n)$,注意边界

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,K=;
int p[N],noww[N],goal[N];
int rnk[N],bkt[N],mul[N][K];
int trs[N][],fth[N],len[N],siz[N],num[N];
char str[N>>],Str[N];
int lth,lst,cnt,tot,mid,maxr;
long long ans;
void link(int f,int t)
{
noww[++cnt]=p[f];
goal[cnt]=t,p[f]=cnt;
}
void DFS(int nde,int fth)
{
mul[nde][]=fth;
for(int i=p[nde];i;i=noww[i])
DFS(goal[i],nde),siz[nde]+=siz[goal[i]];
for(int i=;mul[nde][i];i++)
mul[nde][i]=mul[mul[nde][i-]][i-];
}
void Insert(int ch,int ps)
{
int nde=lst,newn=++tot;
num[ps]=lst=newn,siz[newn]=,len[newn]=len[nde]+;
while(nde&&!trs[nde][ch])
trs[nde][ch]=newn,nde=fth[nde];
if(!nde) fth[newn]=;
else
{
int tran=trs[nde][ch];
if(len[tran]==len[nde]+)
fth[newn]=tran;
else
{
int rnde=++tot; len[rnde]=len[nde]+;
for(int i=;i<=;i++) trs[rnde][i]=trs[tran][i];
fth[rnde]=fth[tran],fth[tran]=fth[newn]=rnde;
while(nde&&trs[nde][ch]==tran)
trs[nde][ch]=rnde,nde=fth[nde];
}
}
}
void prework()
{
register int i,j;
scanf("%s",str+),lth=strlen(str+),lst=tot=;
for(i=;i<=lth;i++) Insert(str[i]-'a',i);
for(i=;i<=tot;i++) link(fth[i],i); DFS(,);
}
void Solve(int l,int r)
{
l=(l+l%)/,r=(r-r%)/;
if(l>r) return ;
int nde=num[r],lth=r-l+;
for(int i=;~i;i--)
if(lth<=len[mul[nde][i]])
nde=mul[nde][i];
ans=max(ans,1ll*lth*siz[nde]);
}
void Manacher()
{
register int i;
int newl=*lth+;
for(i=;i<=newl;i++)
Str[i]=(i&)?'?':str[i>>];
Str[]='>',Str[newl+]='<';
for(i=;i<=newl;i++)
{
fth[i]=(maxr>=i)?min(maxr-i+,fth[*mid-i]):;
Solve(i-fth[i]+,i+fth[i]-);
while(Str[i-fth[i]]==Str[i+fth[i]])
fth[i]++,Solve(i-fth[i]+,i+fth[i]-);
if(i+fth[i]->maxr) maxr=i+fth[i]-,mid=i;
}
}
int main()
{
prework(),Manacher();
printf("%lld",ans);
return ;
}

解题:APIO 2014 回文串的更多相关文章

  1. APIO 2014 回文串(Manacher+后缀自动机+倍增)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=3676 思路 好像还是回文自动机裸体,但是 \(\text{Manacher}\) +后缀自动 ...

  2. 【题解】回文串 APIO 2014 BZOJ 3676 COGS 1985 Manacher+后缀数组+二分

    这题可以用回文自动机来做,但是我并没有学,于是用Manacher+SA的做法O(nlogn)水过 首先,看到回文串就能想到用Manacher 同样还是要利用Manacher能不重复不遗漏地枚举每个回文 ...

  3. leetcode 214. 最短回文串 解题报告

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

  4. 洛谷 P4555 [国家集训队]最长双回文串 解题报告

    P4555 [国家集训队]最长双回文串 题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为\(n\)的串 ...

  5. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  6. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  7. 动态规划——H 最少回文串

    We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...

  8. 动态规划——G 回文串

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  9. 小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

随机推荐

  1. dubbo 微服务

    # spring-dubbo-service 微服务 项目地址:https://github.com/windwant/spring-dubbo-service spring dubbo servic ...

  2. Python基础入门(迭代器和生成器)

    1 Python迭代器 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退. 迭代器有两个基本的方法:iter() 和 ...

  3. Linux建立互信关系(ssh公钥登录)

    Linux有多种登录方式,比如telnet.ssh.支持ssh登录方式:口令登录和公钥登录 ssh登录方式:ssh [-l login_name] [-p port] [user@]hostname ...

  4. ES6的新特性(10)——Class 的基本语法

    Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...

  5. JS 数组方法 array数组声明 元素的添加和删除 等

    声明数组 var arr1 = [1,2,3,4,5]; var arr2 = new Array(100); //声明长度为100的arr2数组. arr2=[]; arr2.length = 10 ...

  6. 20181120-4 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2409 版本控制地址   https://git.coding.net/lg ...

  7. 软件工程-东北师大站-第四次作业PSP

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  8. OGNL动态实现result

    OGNL就是struts.xml文件中的<result>通过get()方法,动态获取action类中的变量 <struts> <package name="de ...

  9. Alpha冲刺——第九天

    Alpha第九天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  10. 周总结<8>

    周次 学习时间 新编代码行数 博客量 学到知识点 15 15 100 1 Html页面设计:虚拟机:(C语言)排序 Html案例: <!DOCTYPE html PUBLIC "-// ...