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. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

  2. android 学习十四 探索安全性和权限

    1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性:    2.1 使用独立进程    2.2 使用固定唯一用户ID    2.3  申明性权限模型   3数字证书   ...

  3. cf#516B. Equations of Mathematical Magic(二进制,位运算)

    https://blog.csdn.net/zfq17796515982/article/details/83051495 题意:解方程:a-(a^x)-x=0 给出a的值,要求计算解(非负)的个数 ...

  4. 【转】UTF8字符串转换为汉字 c#,转自游戏开发主席

    using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...

  5. JDK源码分析:Object.java

    一. 序言 Object.java是一切类的基类,所以了解该类有一定的必要 二 .属性及方法分析 方法列表: private static native void registerNatives(); ...

  6. New Year and Domino:二维前缀和

    题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...

  7. Bitcoin: A Peer-to-Peer Electronic Cash System

    Bitcoin: A Peer-to-Peer Electronic Cash System Satoshi Nakamoto October 31, 2008 Abstract A purely p ...

  8. RDL/RDLC批量单据打印 [转]

    RDL/RDLC批量单据打印 使用RDL或RDLC进行单据打印时,单张单据打印比较直观简单,无需说明.下面我们来谈一下批量单据打印的实现方法.以下以RDL的ReportBuilder设计环境为例进行讲 ...

  9. 5.hadoop常用命令

    1. 单独启动和关闭hadoop服务 启动名称节点 #hadoop-daemon.sh start namenode 启动数据节点 #hadoop-daemons.sh start datanode ...

  10. POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)

    Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...