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 ...
随机推荐
- Shell教程 之test命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 1.数字测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 -ge 大于 ...
- H5入门
1.基本骨架 <!DOCTYPE html> <html> <head><title>标题</title><meta charset= ...
- 方法装饰器(Decorator)
代码: function enhance(target, key, descriptor) { const method = descriptor.value; let extraSpeed = 50 ...
- mysql 索引 create_time 加explain关键字是否走索引
SELECT * FROM t_user WHERE email='217@xxg.com'; --1.725 --加email索引之后 0.003 SELECT * FROM t_user WHE ...
- java_15 System类
1.System类 2.System类方法 (1)currentTimeMillis() public static void main(String[] args) { long start = S ...
- 用java修改文件的编码
1.将本地的文件转换成另外一种编码输出,主要逻辑代码如下: /** * 将本地文件以哪种编码输出 * @param inputfile 输入文件的路径 * @param outfile 输出文件的路径 ...
- Object转为Bigdecimal
import java.math.BigDecimal; import java.math.BigInteger; public class MathUtils { public static Big ...
- Socket(转自 阿里云)
摘要:Socket-C/S Socket又称"套接字",应用程序 通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...
- DOM心得
一.自定义属性值两种方法的注意事项 1.用元素节点.属性(元素节点[属性])绑定的属性值不会出现在标签上. 2.用get/set/removeAttribut(,)等绑定的属性会出现在标签上.且两种方 ...
- PAT 1042 字符统计(20)(思路)
1042 字符统计(20)(20 分) 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格 ...