解题:APIO 2014 回文串
初见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 回文串的更多相关文章
- APIO 2014 回文串(Manacher+后缀自动机+倍增)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=3676 思路 好像还是回文自动机裸体,但是 \(\text{Manacher}\) +后缀自动 ...
- 【题解】回文串 APIO 2014 BZOJ 3676 COGS 1985 Manacher+后缀数组+二分
这题可以用回文自动机来做,但是我并没有学,于是用Manacher+SA的做法O(nlogn)水过 首先,看到回文串就能想到用Manacher 同样还是要利用Manacher能不重复不遗漏地枚举每个回文 ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- 洛谷 P4555 [国家集训队]最长双回文串 解题报告
P4555 [国家集训队]最长双回文串 题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为\(n\)的串 ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- 动态规划——H 最少回文串
We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...
- 动态规划——G 回文串
G - 回文串 Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- 小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
随机推荐
- C语言的函数调用过程(栈帧的创建与销毁)
从汇编的角度解析函数调用过程 看看下面这个简单函数的调用过程: int Add(int x,int y) { ; sum = x + y; return sum; } int main () { ; ...
- 测试面试必会sql(1)
测试一般各种查询语句用的较多,下面的查询语句都是需要熟悉的 Course表 Score表 Student表 Teacher表 1,查询课程编号为“02”的总成绩 SELECT * FROM `Scor ...
- shell 判断日期间隔及润年
#!/bin/bash test.sh until echo "----------------------------------" echo "请输入您的选择:&qu ...
- Unity —— 通过鼠标点击控制物体移动
//ClickMove - - 通过鼠标点击控制物体移动 using System.Collections; using System.Collections.Generic; using Unity ...
- HBASE理论篇
1.Hbase是什么 HBase是一种构建在HDFS之上的分布式.面向列的存储系统.在需要实时读写.随机访问超大规模数据集时,可以使用HBase. 尽管已经有许多数据存储和访问的策略和实现方法,但事实 ...
- [寒假学习笔记](一)Markdown语法学习
Markdown 学习 在博客园上使用markdown编辑,记录学习进度,以来日可以复习 前期准备 1. 安装markdownpad2 官网直接找下载安装,遇到bug他会自动提示信息,跟着提示去安装一 ...
- 特征点检测--基于CNN:TILDE: A Temporally Invariant Learned DEtector
TILDE: A Temporally Invariant Learned DEtector Yannick Verdie1,∗ Kwang Moo Yi1,∗ Pascal Fua1 Vincent ...
- React Native 【学习总结】-【常用命令】
前言 刚接触RN,相信很多人无从下手,不知道下一步要干什么,能干什么,本次学习围绕这个问题,将RN的常用命令总结一下,帮助你快速上手 架构理解 光知道命令的作用,远远不够,如果知道命令背后的意义,才能 ...
- activemq 持久化
转自: http://blog.csdn.net/kobejayandy/article/details/50736479 消息持久性的原理很简单,就是在发送者将消息发送出去后,消息中心首先将消息存储 ...
- c# 捕获一般获取不到的异常
1.主函数入口加异常事件,代码例如: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void M ...