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
題解:
這題走了許多彎路,最後好不容易拍完錯誤,代碼沒有改得精簡.
思路可以參考:
根據上題可以想到,拿每一個串都在A得SAM上跑,然後記錄當前串在SAM得每一個節點的最大值,然後如果是多個串的話
直接記錄最小值即可,最後掃一邊記錄最小值的最大值即可
注意一些細節:
對於沒一個串的匹配,如果當前結點被匹配到了,那麼他的父親結點都可以匹配到,那麼直接拓撲序更新即可,不然就WA#10哦..
 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define il inline
#define RG register
using namespace std;
const int N=;
char s[N];int cur=,cnt=,last=,fa[N],ch[N][],dis[N],n=;
void build(int c)
{
last=cur;cur=++cnt;
RG int p=last;dis[cur]=++n;
for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
if(!p)fa[cur]=;
else{
int q=ch[p][c];
if(dis[q]==dis[p]+)fa[cur]=q;
else{
int nt=++cnt;dis[nt]=dis[p]+;
memcpy(ch[nt],ch[q],sizeof(ch[q]));
fa[nt]=fa[q];fa[q]=fa[cur]=nt;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
}
}
}
int id=,t[N],tot[N],mx[N],sa[N];bool mark[N][];
void FLr()
{
RG int p=,l=strlen(s),c,u,len=;id++;
for(RG int i=;i<l;i++){
c=s[i]-'a';
u=ch[p][c];
if(u){
mark[u][id]=true;len++;
p=u;
}
else{
while(p && !ch[p][c])p=fa[p];
if(p){
mark[ch[p][c]][id]=true;
len=dis[p]+;
p=ch[p][c];
}
else p=,len=;
}
if(len>mx[p])mx[p]=len;
}
for(RG int i=cnt;i;i--){
p=sa[i];
if(mx[p]<tot[p])tot[p]=mx[p];
if(fa[p] && mark[p][id])mx[fa[p]]=dis[fa[p]];
mx[p]=;
}
}
int c[N];
void Dfp(){
RG int p,i,j;
for(i=cnt;i;i--){
p=sa[i];
for(j=;j<=id;j++)
mark[fa[p]][j]|=mark[p][j];
}
for(i=;i<=cnt;i++)
for(j=;j<=id;j++)
if(mark[i][j])t[i]++;
}
int ans=;
void dfs(){
for(int i=;i<=cnt;i++){
if(t[i] && tot[i]>ans)ans=tot[i];
}
}
void jip(){
RG int i;
for(i=;i<=cnt;i++)c[dis[i]]++;
for(i=;i<=n;i++)c[i]+=c[i-];
for(i=cnt;i;i--)sa[c[dis[i]]--]=i;
}
void work()
{
scanf("%s",s);
for(RG int i=,l=strlen(s);i<l;i++)build(s[i]-'a');
for(RG int i=;i<=cnt;i++)tot[i]=N;
jip();
while(~scanf("%s",s))FLr();
Dfp();dfs();
printf("%d\n",ans);
}
int main()
{
freopen("pow.in","r",stdin);
freopen("pow.out","w",stdout);
work();
return ;
}

												

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

  1. SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  2. SPOJ 1812 Longest Common Substring II(后缀自动机)

    [题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...

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

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

  4. 【SPOJ】Longest Common Substring II

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

  5. SPOJ LCS2 - Longest Common Substring II

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

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

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

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  8. SPOJ LCS2 - Longest Common Substring II 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...

  9. Virtual Judge SPOJ - LCS2 Longest Common Substring II

    https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...

随机推荐

  1. 20145237 《Java程序设计》第2周学习总结

    教材学习内容总结 本周我学习了java的基础语法.分为类型.变量与运算符,流程控制. 一.类型:1.Java可以区分为基本类型和类类型.类类型也称作参考类型.2.Java中基本类型主要是整数.字节.浮 ...

  2. hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

    环境:weblogic10.3.5,hibernate3,GGTS(groovy/grails tools suite):出现这问题是因为该项目是从weblogic8.1.6下移植到weblogic1 ...

  3. 【技巧】Java工程中的Debug信息分级输出接口

    也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...

  4. 深入理解java的static关键字

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  5. linux的链接工具secure设置字体大小和颜色

  6. jupyter notebook下python2和python3共存(Ubuntu)

    提示NOTICE 时间:2018/04/06 主题:Ubuntu 下CAFFE框架 主角:Jupyter Notebook 简介: Jupyter Notebook(此前被称为 IPython not ...

  7. Spark快速入门

    Spark 快速入门   本教程快速介绍了Spark的使用. 首先我们介绍了通过Spark 交互式shell调用API( Python或者scala代码),然后演示如何使用Java, Scala或者P ...

  8. Ecplise 配置本地 https 测试

    今天做项目,需要关联Office 365.为了实现Office365的用户邮件信息与项目的实时同步,需要建立webhook订阅. Office 365 API 连接  https://graph.mi ...

  9. Web开发笔记

    jquery ui draggable clone之后不会克隆draggable功能,要重新设置

  10. [转]最常用的15大Eclipse开发快捷键技巧

    作者:Java我人生(陈磊兴)   原文出处http://blog.csdn.net/chenleixing/article/details/44600587 做Java开发的,经常会用Eclipse ...