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. Java学习笔记(1)——基本数据类型

    一.进制转换 10^n被称为权  10称为基数   计算机中正数和负数的关系是取反加一, 如: ~3+1=-3 补码边界运算有溢出风险 32位二进制补码最多表示2^32个数, -2G~2G 1,计算机 ...

  2. javascript中子类如何继承父类

    参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/inheritance.html#toc4 function Shape() { this.x = 0; t ...

  3. office2007序列号/密钥

    绝对可以用的许可证:V9MTG-3GX8P-D3Y4R-68BQ8-4Q8VD

  4. # MongoDB学习笔记(持续更新)

    启动mongo服务 sodo mongo 显示数据库(显示数据库名称和大小,单位GB) > show dbs admin (empty) local 0.078GB test 0.078GB t ...

  5. 【C#多线程】C#多线程 Thread 开发基础

    引用 using System; using System.Threading; 多线程代码 Thread mainthread = new Thread(ExecuteThread); mainth ...

  6. struts2.1.*中再实现了一个servlet的方法

    学习Struts2也有一段时间了,今天用Servlet写了一个验证码,然后搬到Struts2中,惊奇地发现Servlet无法访问,出现404错误!后来折腾了半天,终于找出原因了.这也算我学习中的一个重 ...

  7. mac os使用lsusb命令和连接未知的Android设备

    今天在mac上连接一个android设备发现连不上,adb devices看不到设备.于是想用lsusb命令看下,结果发现Mac居然没有这个命令,于是网上搜了下.发现了以下的命令system_prof ...

  8. [Redux] React Todo List Example (Toggling a Todo)

    /** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...

  9. Mysql数据库的mysql Schema 究竟有哪些东西&amp; 手工注入的基础要领

    #查看数据库版本号 mysql> select @@version; +------------+ | @@version  | +------------+ | 5.5.16-log | +- ...

  10. mybati之parameterType传递多个参数

    当在查询的时候需要传入多个参数的时候该怎么办呢: 1,封装成一个Model对象,底层HashMap还是一个 User user=new User(); user.setUserName("z ...