Description

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

题意:给若干个字符串,长度不超过100000,求最长公共连续字串
解析:后缀自动机,对输入的第一个字符串建一个后缀自动机,在
sam中添加两个量Max[i](查询时以第i个节点为最后一个字符的后缀的最大长度)
,Min[i](所有查询Max[i]中的最小值)。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=;
struct SAM
{
int ch[maxn][];
int pre[maxn],step[maxn];
int last,id;
int Min[maxn],Max[maxn];
void init() //初始化
{
last=id=;
memset(ch[],-,sizeof(ch[]));
pre[]=-; step[]=;
Min[]=Max[]=;
}
void Insert(int c) //模板,自己百度
{
int p=last,np=++id;
step[np]=step[p]+;
memset(ch[np],-,sizeof(ch[np]));
Min[np]=Max[np]=step[np];
while(p!=-&&ch[p][c]==-) ch[p][c]=np,p=pre[p];
if(p==-) pre[np]=;
else
{
int q=ch[p][c];
if(step[q]!=step[p]+)
{
int nq=++id;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
step[nq]=step[p]+;
Min[nq]=Max[nq]=step[nq];
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while(p!=-&&ch[p][c]==q) ch[p][c]=nq,p=pre[p];
}
else pre[np]=q;
}
last=np;
}
void Find(char *S)
{
int len=strlen(S);
int u=,d=;
for(int i=;i<=id;i++) Max[i]=;
for(int i=;i<len;i++)
{
int c=S[i]-'a';
if(ch[u][c]!=-) d++,u=ch[u][c];
else
{
while(u!=-&&ch[u][c]==-) u=pre[u];
if(u!=-) d=step[u]+,u=ch[u][c];
else u=,d=;
}
Max[u]=max(Max[u],d);//更新
}
for(int i=id;i>=;i--) Max[pre[i]]=max(Max[pre[i]],Max[i]);
for(int i=;i<=id;i++) Min[i]=min(Min[i],Max[i]);
}
int GetAns()
{
int ret=;
for(int i=;i<=id;i++) ret=max(ret,Min[i]);
return ret;
}
}sam;
char S[maxn];
int main()
{
scanf("%s",S);
int len=strlen(S);
sam.init();
for(int i=;i<len;i++) sam.Insert(S[i]-'a');
while(scanf("%s",S)!=EOF) sam.Find(S);
printf("%d\n",sam.GetAns());
return ;
}

spoj1812-Longest Common Substring II(后缀自动机)的更多相关文章

  1. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

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

  3. 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)

    传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...

  4. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

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

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

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

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

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

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

  8. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

  9. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

随机推荐

  1. iframe切换内容页仍然能自适应大小代码(含js)

    function setIframeHeight(iframe) { if (iframe) { var iframeWin = iframe.contentWindow || iframe.cont ...

  2. iperf网络测试工具

    iperf https://sourceforge.net/projects/iperf/ http://downloads.es.net/pub/iperf/ https://github.com/ ...

  3. JS-Array数组内置对象

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  4. (转)重置Mac OS X管理员密码

    忘记Mac管理员密码怎么办?别担心,办法总会有的. [方法一] 开机按住option,选择Recovery HD(Snow Leopard插入光盘开机按住C) Snow Leopard系统:进入后在上 ...

  5. C++编程规范之20:避免函数过长,避免嵌套过深

    摘要: 短胜于长,平胜于优,过长的函数和嵌套过深的代码块的出现,经常是因为没能赋予一个函数以一个紧凑的职责所致,这两种情况通常都能够通过更好的重构予以解决. 每个函数都应该顾其名而能知其义,易于理解的 ...

  6. SQL 查询某字段id为空(不为空)

    1 sql 查询某字段id为空 select *  from  表名 where  id  is   null  ; 2 sql 查询某字段id不为空 select * from   表名  wher ...

  7. c# WPF 项目优化

    业务流程图 优化前后对比: 优化过程: 1. 界面刷新,特别是表格刷新 把ListView.DataContext  = DataSet 这些代码替换成以下: public static void S ...

  8. 配置NFS服务

    1. NFS配置,需要安装哪些包?nfs-utils  和 rpcbind2. 如果不开启rpcbind服务,就启动NFS,会怎么样?如果不开启rpcbind服务,会报错:rpc.nfsd: writ ...

  9. NHibernate之映射文件配置说明(转载1)

    源博客:http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 1. hibernate-mapping 这个元素包括以下可选 ...

  10. (转)url重写

    使用URLRewriter.dll后,根本不需要使用任何代码,我之前做的项目就是用的做URL重写的,其实不是进化,其实表面上看是.html扩展名而已,当然你还可以用其他的任意扩展名下面是你的配置 &l ...