A string is finite sequence of characters over a non-empty finite set \(\sum\).

In this problem, \(\sum\) 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

Notice: new testcases added

题意:

给出多个字符串,求多个串的最长公共子串

题解:

把第一个串建一个后缀自动机,之后把每个串都在上面跑一个\(LCS\)如这个,跑的时候记录每一个点的最大匹配长度。每跑完一个串把当前答案在\(parent\)树上反向拓扑一下,更新每个点在所有答案中的最小答案。最后再取个最大值就行了。

#include<bits/stdc++.h>
using namespace std;
const int N=200010;
char s[N];
int a[N],c[N];
void cmax(int &a,int b){
a=max(a,b);
}
void cmin(int &a,int b){
a=min(a,b);
}
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1],mx[N<<1],mn[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
size[np]=1;
}
void build(char s[]){
memset(mn,0x3f,sizeof mn);
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void calc(){
for(int i=1;i<=cnt;++i)c[l[i]]++;
for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
}
void work(char s[]){
int len=strlen(s+1);
int p=1,left=0,as=0;
while(left<=len){
left++;
while(p&&(!ch[p][s[left]-'a']))p=fa[p],as=l[p];
if(!p)p=1,as=0;
else{
as++;
p=ch[p][s[left]-'a'];
cmax(mx[p],as);
}
}
for(int i=cnt;i;--i){
int p=a[i],f=fa[p];
cmax(mx[f],min(mx[p],l[f]));
cmin(mn[p],mx[p]);mx[p]=0;
}
}
void tj(){
int ot=0;
for(int i=1;i<=cnt;++i)
cmax(ot,mn[i]);
cout<<ot<<endl;
}
}sam;
int main(){
cin>>s+1;
sam.build(s);int js=0,ll=strlen(s+1);
sam.calc();
while(cin>>s+1)
sam.work(s);
sam.tj();
}

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

  1. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

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

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

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

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

  4. spoj1812 LCS2 - Longest Common Substring II

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

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

  6. SPOJ LCS2 - Longest Common Substring II

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

  7. 【SP1812】LCS2 - Longest Common Substring II

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

  8. 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II

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

  9. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

随机推荐

  1. C#控制台自定义背景颜色,字体颜色大全

    效果: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  2. ASP.NET MVC HtmlHelper 默认值

    例如: @Html.HiddenFor(m=>m.IsMating,new { Value="True"}) 注意:value属性的首字母要用大写.

  3. program by the way......

    ostrich birds fruit apple constructor height weight method overload override base sub inherit extend ...

  4. Java数据结构和算法(四)赫夫曼树

    Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...

  5. 深入浅出 JMS(一) - JMS 基本概念

    深入浅出 JMS(一) - JMS 基本概念 一.JMS 是个什么鬼 JMS 是 Java Message Service 的简称,即 Java 消息服务.什么是消息服务呢,我们来看一下 Oracle ...

  6. 在cygwin中执行bat文件

    在cygwin中执行bat文件 #!/bin/bash dir_gen_image_bat=C:/MinGW/msys/1.0/home/Administrator/feitian_audio/FOT ...

  7. java 事件监听

    事件监听实现: 三要素: 1.事件源(数据源,要处理的数据) 2.事件 (承载数据,传递信息并被监听) 3.监听器 (负责对数据的业务处理) --该开发用例采用了Spring的事件监听 1.  定义事 ...

  8. python 线程和进程概述

    计算机中执行任务的最小单元:线程 IO操作利用CPU GIL,全局解释器锁 IO密集型: 多线程(不用CPU) 计算机密集型(用CPU) 进程和线程的目的:提高执行效率 1.单进程单线程,主进程.主线 ...

  9. gj6 深入python的set和dict

    6.1 collections中的abc from collections.abc import Mapping, MutableMapping #dict属于mapping类型 a = {} pri ...

  10. Java中BufferedReader和scanner

    Scanner 和BufferedReader同样能实现将键盘输入的数据送入程序, import java.io.*;import java.util.Scanner;public class C { ...