A string is finite sequence of characters over a non-empty finite set Σ.

In this problem, Σ is the set of lowercase letters.

Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.

Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.

Here common substring means a substring of two or more strings.

Input

The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.

Output

The length of the longest common substring. If such string doesn't exist, print "0" instead.

Example

Input:

alsdfkjfjkdsal

fdjskalajfkdsla

aaaajfaaaa

Output:

2

Solution

提出一个串做SAM

做完后,将剩下的所有串与SAM进行匹配

每次匹配时记录每个节点以它为结尾最长能够匹配多长,然后将这次匹配的结果与保存的最后的答案取min(我们要保证剩下的所有的串与第一个串匹配的是同一段)

全部匹配完后在每个节点取max就是答案了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10+5,MAXS=100000+10,inf=0x3f3f3f3f;
int n,las=1,tot=1,len[MAXS<<1],fa[MAXS<<1],ch[MAXS<<1][30],ans,st[MAXS<<1],rk[MAXS<<1],cnt[MAXS],now[MAXS<<1];
char s[MAXN][MAXS];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
int p=las,np=++tot;
las=np;
len[np]=len[p]+1;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=1;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1)fa[np]=q;
else
{
int nq=++tot;
fa[nq]=fa[q];
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
len[nq]=len[p]+1,fa[np]=fa[q]=nq;
while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
}
inline int match(int w)
{
memset(now,0,sizeof(now));
for(register int i=1,j=1,res=0,lt=strlen(s[w]+1);i<=lt;++i)
{
int c=s[w][i]-'a'+1;
if(ch[j][c])res++,j=ch[j][c];
else
{
while(j&&!ch[j][c])j=fa[j];
if(!j)res=0,j=1;
else res=len[j]+1,j=ch[j][c];
}
chkmax(now[j],res);
}
for(register int i=tot;i>=1;--i)chkmax(now[fa[rk[i]]],now[rk[i]]);
for(register int i=1;i<=tot;++i)chkmin(st[i],now[i]);
}
int main()
{
while(scanf("%s",s[++n]+1)!=EOF);
n--;
for(register int i=1,lt=strlen(s[1]+1);i<=lt;++i)extend(s[1][i]-'a'+1);
for(register int i=1;i<=tot;++i)st[i]=len[i];
for(register int i=1;i<=tot;++i)cnt[len[i]]++;
for(register int i=1,lt=strlen(s[1]+1);i<=lt;++i)cnt[i]+=cnt[i-1];
for(register int i=1;i<=tot;++i)rk[cnt[len[i]]--]=i;
for(register int i=2;i<=n;++i)match(i);
for(register int i=2;i<=tot;++i)chkmax(ans,st[i]);
write(ans,'\n');
return 0;
}

【刷题】SPOJ 1812 LCS2 - Longest Common Substring II的更多相关文章

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

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

  2. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  3. SPOJ 1812 LCS2 - Longest Common Substring II

    思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...

  4. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  5. SPOJ:LCS2 - Longest Common Substring II

    题面 给定一些字符串,求出它们的最长公共子串 输入格式 输入至多 \(10\) 行,每行包含不超过 \(100000\)个的小写字母,表示一个字符串 输出格式 一个数,最长公共子串的长度 若不存在最长 ...

  6. spoj1812 LCS2 - Longest Common Substring II

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

  7. SPOJ LCS2 - Longest Common Substring II

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

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

  9. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

随机推荐

  1. js三种存储方式区别

    javaScript有三种数据存储方式,分别是: sessionStorage localStorage cookier 相同点:都保存在浏览器端,同源的 不同点: ①传递方式不同 cookie数据始 ...

  2. gdb 分析出错

    1 创建测试代码test.php <?php function test1(){ while(true){ sleep(1); } }echo getmypid() "\r\n&quo ...

  3. python学习五

    打包代码与数据 数据结构要与数据匹配,数据结构影响代码的复杂性   列表 集合 字典 #创建与初始化 cleese={} cleese2=dict() cleese["name"] ...

  4. unity3d 角色头顶信息3D&2D遮挡解决方案(一)

    先上效果图,只凭文字描述,脑补应该有些困难- - 如图:有三个角色(我们暂且从左到右叫它们A.B.C),一个2D UI(中间动作选择的框框),一个cube(右边的方块) cube挡住了角色C的头顶信息 ...

  5. Chromium添加一段新字符串

    参考:https://groups.google.com/a/chromium.org/forum/#!searchin/chromium-dev/tclib%7Csort:relevance/chr ...

  6. photoshop cc 2018安装破解教程(破解补丁,亲测,绝对可用)

    破解步骤说明:下载地址百度网盘,https://pan.baidu.com/s/1cWtpUesl2fms3tFwEC0MiQ 1.右键解压Adobe Photoshop CC 2018 64位这个文 ...

  7. leetcode-组合总数III(回溯)

    组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合.  示例 ...

  8. 【20180807模拟测试】tree

    题目描述 或许会传送失败的传送门 #分析 考虑如何才能让白边显得更(不)重要,即在每条白边上(加上)减去一个值. 我们可以二分这个值,然后用寻常方法做最小生成树.统计在此最小生成树里有多少白 边. 然 ...

  9. jmeter链接数据库问题汇总

    1.最新驱动下载: 驱动版本与mysql服务不兼容也是会报错的 下载地址:https://dev.mysql.com/downloads/connector/j/ 打开页面一直拉到页面底部,此处选择P ...

  10. n! 阶乘

    其实1.2.3.4.6.7…都是可以不用考虑的,因此选择以5为迭代步数即可. 首先,这些数字都可以不用进行%5(对5取余数)运算,因此每次循环时可以直接将函数的count变量直接加1.其次,考虑25. ...