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 ...
随机推荐
- LinearLayout 线性布局
android:orientation 设置布局管理器内组件的排列方式,可设置为 horizontal (水平排列).vertical (垂直排列) android:gravity 设置布局管理器内组 ...
- 21 pythone【入门指南】:string
string是很基础的数据结构,来看看string有哪些常用的操作. #!/bin/python #!---encoding: UTF- s = 'abcdefg' #concat s1 = s + ...
- VS PDB文件
转 PDB文件 PDB(Program Data Base),意即程序的基本数据,是VS编译链接时生成的文件.DPB文件主要存储了VS调试程序时所需要的基本信息,主要包括源文件名.变量名.函数名.FP ...
- Django的Modelforms的介绍
from django.forms import ModelForm class Test(ModelForm): # 把那张表转化成form组件 class Meta: # 这个意思即是把Artic ...
- fastjson的常用方法
/** * 解析对象形式的json字符串 */ public static void test1() { String jsonStr = "{\"JACKIE_ZHANG\&qu ...
- .net core和.net 4.7区别和联系笔记
1. 简单说,都是.net standard所定义的接口的实现,都是 .net standard的儿子. 3down voteaccepted C# is a programming language ...
- blockchain good article
https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3 ...
- N! (数组)
#include <iostream> using namespace std; ; int f[MAXN]; int main(){ int n; cin >> n; f[] ...
- iOS.Notification.Bar.Color
Reference: http://apple.stackexchange.com/questions/44246/what-determines-the-special-color-of-the-s ...
- holiday(假期)_题解
holiday(假期) —— 一道妙题(codevs3622) Description 经过几个月辛勤的工作,FJ 决定让奶牛放假.假期可以在1…N 天内任意选择一段(需要连续),每一天都有一个享 ...