2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门
后缀自动机基础题。
给出10个串求最长公共子串。
我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可。
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
const int N=2e5+5;
int T=0,n;
char s[N];
struct SAM{
int rt,tot,last,len[N],lz[N],cnt[N],rk[N],val[N],link[N],son[N][26],mn[N],mx[N];
SAM(){rt=tot=last=1,len[0]=-1,fill(son[0],son[0]+26,1);}
inline void expend(int x){
int p=last,np=++tot;
last=np,len[np]=len[p]+1;
while(p&&!son[p][x])son[p][x]=np,p=link[p];
if(!p){link[np]=rt;return;}
int q=son[p][x],nq;
if(len[q]==len[p]+1){link[np]=q;return;}
len[nq=++tot]=len[p]+1,memcpy(son[nq],son[q],sizeof(son[q])),link[nq]=link[q];
while(p&&son[p][x]==q)son[p][x]=nq,p=link[p];
link[np]=link[q]=nq;
}
inline void topsort(){
for(ri i=1;i<=tot;++i)mn[i]=len[i];
for(ri i=1;i<=tot;++i)++cnt[len[i]];
for(ri i=1;i<=last;++i)cnt[i]+=cnt[i-1];
for(ri i=1;i<=tot;++i)rk[cnt[len[i]]--]=i;
}
inline void update(){
int p=1,nowlen=0;
for(ri i=1;i<=tot;++i)mx[i]=0;
for(ri i=1,x;i<=n;++i){
x=s[i]-'a';
if(son[p][x])p=son[p][x],++nowlen;
else{
while(!son[p][x])p=link[p];
nowlen=len[p]+1,p=son[p][x];
}
mx[p]=max(mx[p],nowlen);
}
for(ri p,i=tot;i;--i)p=rk[i],mn[p]=min(mn[p],mx[p]),mx[link[p]]=min(len[link[p]],max(mx[link[p]],mx[p]));
}
inline void query(){
int ans=0;
for(ri i=1;i<=tot;++i)ans=max(ans,mn[i]);
if(ans<2)ans=0;
cout<<ans;
}
}sam;
int main(){
while(~scanf("%s",s+1)){
++T,n=strlen(s+1);
if(T==1){for(ri i=1;i<=n;++i)sam.expend(s[i]-'a');sam.topsort();}
else sam.update();
}
sam.query();
return 0;
}
2018.12.15 spoj Longest Common Substring II(后缀自动机)的更多相关文章
- 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)
传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
随机推荐
- XFF的学习+修改源码--Are you in class
这几天有做天枢CTF的“Are you in class”的题目,虽然以前了解过XFF,但还是没有很好地应用,而且最后居然掉进了一个大坑,且听我细细讲来. 打开题目,首先有个提示“在不在学校主要看 ...
- 微信小程序接入百度统计
一. 百度统计添加应用,获取appkey和微信小程序统计sdk: 1. 百度统计首页,点击“我的全部应用”右侧的添加按钮: 2. “应用类型”选择小程序统计,选择微信小程序,填写应用名称信息,选择内容 ...
- [剑指Offer]59-队列的最大值(题目二待补)
题目一:滑动窗口的最大值 题目链接 https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqI ...
- Django的rest_framework的视图之Mixin类编写视图源码解析
Mixin类编写视图 我们这里用auther表来做演示,先为auther和autherdetail写2个url url(r'^autherdetail/(?P<id>\d+)', view ...
- 开机进入boot menu和application menu,无法开机
1.开机点击F1进入到bios界面 2.进入Security—Secure Boot—Disabled 如果不修改Secure boot选项为Disabled,在光驱引导时可能会出现报错 3. ...
- Xcode 去掉控制台无用打印信息
1. 2.在Environment Variables增加一键值对 OS_ACTIVITY_MODE = disable 转自:https://blog.csdn.net/HelloWorld_198 ...
- java 线程Thread 技术--线程创建源码解释
永远不要忘记最基础的东西,只有把最基础的知识打牢靠,才能够使你走的更远,我将从今天开始,进行线程知识的回顾,一些常用知识点,以及java1.5 引入的并发库,进行详细的讲解与总结 创建线程的目的是为了 ...
- 先安装win7时IIS的安装
打开“控制面板”->选择“程序”->选择“打开或关闭windows功能”->在“Internet信息服务”中勾选以下勾选框
- ---转载---phython资料
整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. https://st ...
- 安装mysql时启动服务出错问题
mysql安装最后一步 无法启动服务错误 博客分类: IDE问题解析 今天安装mysql程序时候,在安装到最后一步时候,在最后一步却发现无法启动服务,出现这样的提示“cannot create ...