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. Android源码分析之Handler

    接上一篇分析,正如Android doc所说,Handler主要有2方面用处: 1. delay执行同一线程中的某个操作,也就是schedule message.runnable在未来的某一时刻执行: ...

  2. css sprite简便方法切 《评分五角星》

    摘抄自我趣同伴的心得: 大家可以会遇到过要做满意度用星级来评分的情况,类似这种 实现的方法有很多,大家最初想到的可能是根据满意度有多少种情况就切多少种图,然后拼在一张图里面,通过控制图片的位置来实现. ...

  3. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分的解决办法方案

    最近操作系统不断地报异常或错误,可能是用得太久了,而刚好工作比较多又不想重新安装系统,但用一段时间后(一天大概有两次)系统就出现各种问题导致无法继续使用真是痛苦万分啊,今天又来了这样的错误: 此实现不 ...

  4. 把你的Project发布到GitHub上

    在上一篇文章中说明了如何使用远程仓库,接下来,就使用常用远程仓库GitHub来管理你的project. 1)在GitHub上创建仓库 要使用GitHub,肯定要注册GitHub帐户,然后建立一个仓库. ...

  5. 大型web系统数据缓存设计

    1. 前言 在高访问量的web系统中,缓存几乎是离不开的:但是一个适当.高效的缓存方案设计却并不容易:所以接下来将讨论一下应用系统缓存的设计方面应该注意哪些东西,包括缓存的选型.常见缓存系统的特点和数 ...

  6. [Java入门笔记] Java语言基础(五):数组

    简介 数组可用用于存储存储多个数据,Java的数组要求所有的数组元素具有一种相同的数据类型.一旦数组初始化完成,数组在内存中的空间被固定下来,长度不可改变,即使把数组的元素清空,所占用的空间依然被保留 ...

  7. Android 分享到微信

    /** * 分享信息到朋友 * * @param file,假如图片的路径为path,那么file = new File(path); */ private void shareToFriend(Fi ...

  8. gdb进程调试,多进程调试

    1.单进程的调试 常规的通过gdb cmd这种方式开启调试,特别说明的是通过attach的方法附加到一个指定的进程上去进行调试,这种方法适合于调试一个已经运行的进程,具体用法:  gdb -p [pi ...

  9. Entity Framework 6 with MySql

        MySQL Connector/Net 6.8.x MySQL Server 5.1 or above Entity Framework 6 assemblies .NET Framework ...

  10. BZOJ1798: [Ahoi2009]Seq 维护序列seq[线段树]

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5504  Solved: 1937[Submit ...