题目连接:戳我

题目大意:求n个字符串的最长公共子串。

它的简化版——这里

当然我们可以用SA写qwq,也可以用广义SAM写qwq

这里介绍纯SAM的写法。。。就是对其中一个建立后缀自动机,然后剩下的N-1个往上面匹配。

设\(sum[i]\)表示到以节点i为根的子树中,最长能够匹配到的最长的子串的长度。

匹配和它的弱化版基本一样,就是注意每次在parent tree上要用拓扑序从下往上更新答案。

注意每个点最大的匹配不能超过它所在类的longest

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000000
using namespace std;
int T,k,last=1,tot=1;
int a[MAXN],c[MAXN],ans[MAXN],sum[MAXN];
char s[MAXN];
struct Node{int son[26],ff,len;}t[MAXN<<1];
inline void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++) extend(s[i]-'a');
for(int i=1;i<=tot;i++) c[t[i].len]++;
for(int i=1;i<=tot;i++) c[i]+=c[i-1];
for(int i=1;i<=tot;i++) a[c[t[i].len]--]=i;
for(int i=1;i<=tot;i++) ans[i]=t[i].len;
while(scanf("%s",s+1)!=EOF)
{
len=strlen(s+1);
memset(sum,0,sizeof(sum));
int cur_ans=0;
int now=1;
for(int i=1;i<=len;i++)
{
if(t[now].son[s[i]-'a']) cur_ans++,now=t[now].son[s[i]-'a'];
else
{
while(now&&!t[now].son[s[i]-'a']) now=t[now].ff;
if(!now) cur_ans=0,now=1;
else cur_ans=t[now].len+1,now=t[now].son[s[i]-'a'];
}
sum[now]=max(sum[now],cur_ans);
}
for(int i=tot;i>=1;i--)
{
int cur=a[i];
sum[t[cur].ff]=max(sum[t[cur].ff],min(t[t[cur].ff].len,sum[cur]));
}
for(int i=1;i<=tot;i++) ans[i]=min(ans[i],sum[i]);
}
int maxx=0;
for(int i=1;i<=tot;i++) maxx=max(maxx,ans[i]);
printf("%d\n",maxx);
return 0;
}

SPOJ Longest Common Substring II的更多相关文章

  1. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  2. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

  3. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  4. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  5. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  6. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  7. 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 ...

  8. Longest Common Substring II SPOJ - LCS2 (后缀自动机)

    Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...

  9. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

随机推荐

  1. php5.4 trait 理解与学习

    Trait 是 php5.4引入的新特性,手册上说的一大段没看懂,这里直接来过来. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制,使开发人员 ...

  2. sobel 使用说明

    转自http://www.cnblogs.com/justany/archive/2012/11/23/2782660.html OpenCV 2.4+ C++ 边缘梯度计算 2012-11-23 0 ...

  3. golang通过反射动态调用方法

    func Call(m map[string]interface{}, name string, params ...interface{}) ([]reflect.Value, error) { f ...

  4. C&C++内存布局

    1. Memory Layout of a C Program http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-p ...

  5. 关于某一discuz 6.0论坛故障的记录

    某日,额突然发现公司的discuz 6.0论坛在IE6浏览器下面显示出现问题: 每个链接都出现了下划线,很难看,不过已访问的链接(a:visted)却没有下划线,于是怀疑css.htm有问题.于是对c ...

  6. C语言dos程序源代码分享(进制转换器)

    今天给大家分享一个dos程序的源代码 这个程序是本人在学习中的经验分享 如果有问题或者建议,欢迎大家一起交流 源代码: /*本程序为一个进制转换器 本程序不作为商业用途,完全为技术交流 喜欢C语言的同 ...

  7. Tomcat之Windows环境下配置多个服务器

    在应对多项目多端口的情况配置一个服务器是远不能满足开发条件的.例如微信公众号回调域名只接受80端口,而其他项目一般为默认的8080或者自定义的其他的端口. 废话多说,直入主题 准备条件:tomcat文 ...

  8. 2018.10.12 NOIP模拟 数据结构(线段树)

    传送门 sb线段树题居然还卡常. 修改操作直接更新区间最小值和区间标记下传即可. 询问加起来最多5e65e65e6个数. 因此直接询问5e65e65e6次最小值就行了. 代码

  9. csdn的一次回答问题

    #coding:utf8 import tushare as ts import pandas as pd import numpy as np import pymysql,datetime imp ...

  10. 8.7 使用索引-notes

    七 正确使用索引 一 索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果,我们在添加索引时,必须遵循以下问题 1 范围问题,或者说条件不明确,条件中出现这 ...