LCS2 - Longest Common Substring II

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

求若干个字符串的最长公共子串。

显然,如果是所有字符串的最长公共子串,至少得是第一个字符串的一个子串,所以对第一个字符串建后缀自动机,维护所有子串。然后把每个其他的字符串扔进去跑一边,找出来最远的公共点即可。

 #include <bits/stdc++.h>

 const int maxn = 1e6 + ;

 /* AUTOMATON */

 int last;
int tail;
int mini[maxn];
int maxi[maxn];
int step[maxn];
int fail[maxn];
int next[maxn][]; inline void buildAutomaton(char *s)
{
last = ; tail = ;
while (*s)
{
int c = *s++ - 'a';
int p = last;
int t = tail++;
step[t] = step[p] + ;
mini[t] = maxi[t] = step[t];
while (p && !next[p][c])
next[p][c] = t, p = fail[p];
if (p)
{
int q = next[p][c];
if (step[q] == step[p] + )
fail[t] = q;
else
{
int k = tail++;
fail[k] = fail[q];
fail[q] = fail[t] = k;
step[k] = step[p] + ;
mini[k] = maxi[k] = step[k];
memcpy(next[k], next[q], sizeof(next[k]));
while (p && next[p][c] == q)
next[p][c] = k, p = fail[p];
}
}
else
fail[t] = ;
last = t;
}
} inline void searchAutoMaton(char *s)
{
memset(maxi, , sizeof(maxi));
for (int t = , k = ; *s; )
{
int c = *s++ - 'a';
if (next[t][c])
++k, t = next[t][c];
else
{
while (t && !next[t][c])
t = fail[t];
if (t)
k = step[t] + , t = next[t][c];
else
k = , t = ;
}
if (maxi[t] < k)
maxi[t] = k;
}
for (int i = tail - ; i; --i)
if (maxi[fail[i]] < maxi[i])
maxi[fail[i]] = maxi[i];
for (int i = ; i < tail; ++i)
if (mini[i] > maxi[i])
mini[i] = maxi[i];
} inline int calculate(void)
{
register int ret = ;
for (int i = ; i < tail; ++i)
if (ret < mini[i])
ret = mini[i];
return ret;
} /* MAIN FUNC */ char str[maxn]; signed main(void)
{
scanf("%s", str);
buildAutomaton(str);
while (~scanf("%s", str))
searchAutoMaton(str);
printf("%d\n", calculate());
}

@Author: YouSiki

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

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

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

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

  3. Virtual Judge SPOJ - LCS2 Longest Common Substring II

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

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

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

  5. spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】

    多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...

  6. SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解

    题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...

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

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

  8. spoj1812 LCS2 - Longest Common Substring II

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

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

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

随机推荐

  1. JDBC快速入门

    /** * JDBC快速入门: * 1.导入jar包 数据库驱动 * 2.注册驱动 * 3.获取数据库连接对象 Connection * 4.定义sql语句 * 5.获取发送执行sql语句的对象 St ...

  2. 基于Ruby的watir-webdriver自动化测试方案与实施(三)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(二) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ...   编写脚本 ...

  3. 解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序

    在Windows Server 2008 x64 上部署一个Vs 2008开发的.net2.0 的asp.net web 程序,调用了office的组件来导入导出excel文件,其中托管管道模式为集成 ...

  4. Azure机器学习入门(一)

    我们开始深入学习Azure机器学习的基本原理并为您开启伟大的数据科学之门.Azure 机器学习的一个重要特征就是在构建预测分析方案时,它能够方便地将开发模式集成为可重复的工作流模式.这就使得Azure ...

  5. struts2案例

    Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点.Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架下开发的程序提供很好的兼容性.下 ...

  6. oracle异常(-)

    一.概述异常分成三大类:预定义异常.非预定义异常.自定义异常处理方法分为:直接抛出异常.内部块处理异常.游标处理异常 预定义异常:由PL/SQL定义的异常.由于它们已在standard包中预定义了,因 ...

  7. NOIP模拟赛20161023

    题目名 双色球 魔方 czy的后宫 mex 源文件 ball.cpp/c/pas cube.cpp/c/pas harem.cpp/c/pas mex.cpp/c/pas 输入文件 ball.in c ...

  8. Redis的安装

    1. 中文官网:http://www.redis.cn/download.html 英文官网:http://www.redis.io/download 里面的内容的一样的,就是一个是中文写的,一个是英 ...

  9. 一分钟搞定AlloyTouch图片轮播

      一分钟搞定AlloyTouch图片轮播 轮播图也涉及到触摸和触摸反馈,同时,AlloyTouch可以把惯性运动打开或者关闭,并且设置min和max为运动区域,超出会自动回弹.除了一般的竖向滚动,A ...

  10. jmeter(三)Sample之SOAP/XML-RPC Request

    项目背景:公司的微信端H5界面新开发了会员注册功能,需要对其进行压力测试 项目目标:需要承受每分钟最少6000的压力 一.建立一个测试计划(test plan) 之前有说过,jmeter打开后会自动生 ...