地址:http://www.spoj.com/problems/LCS2/

题面:

LCS2 - Longest Common Substring II

no tags 

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

Notice: new testcases added

思路:

  对第一个穿构建sam,然后让后面的串在sam上走,同时记录每个串跑完后各个状态的所能匹配的最长长度,然后对所有串取个最小值,之后再取个最大值。

  注意:如果状态p能够到达,那么状态fa[p]也必然能够到达,但可能在匹配时不会走到fa[p]状态,所以当p可达时,要更新fa[p]。

 #include <bits/stdc++.h>

 using namespace std;

 struct SAM
{
static const int MAXN = <<;//大小为字符串长度两倍
static const int LetterSize = ; int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
int sum[MAXN], tp[MAXN]; //用于拓扑排序,tp为排序后的数组 void init( void)
{
last = tot = ;
len[] = ;
memset(ch,,sizeof ch);
memset(fa,,sizeof fa);
} void add( int x)
{
int p = last, np = last = ++tot;
len[np] = len[p] + ;
while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if( p == )
fa[np] = ;
else
{
int q = ch[p][x];
if( len[q] == len[p] + )
fa[np] = q;
else
{
int nq = ++tot;
memcpy( ch[nq], ch[q], sizeof ch[q]);
len[nq] = len[p] + , fa[nq] = fa[q], fa[q] = fa[np] = nq;
while( p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
}
}
} void toposort( void)
{
for(int i = ; i <= len[last]; i++) sum[i] = ;
for(int i = ; i <= tot; i++) sum[len[i]]++;
for(int i = ; i <= len[last]; i++) sum[i] += sum[i-];
for(int i = ; i <= tot; i++) tp[sum[len[i]]--] = i;
}
} sam;
char ss[];
int ans,mx[<<],mi[<<]; int main(void)
{
//freopen("in.acm","r",stdin);
sam.init();
scanf("%s",ss);
for(int i=,len=strlen(ss);i<len;i++) sam.add(ss[i]-'a');
for(int i=;i<=sam.tot;i++) mi[i]=1e6;
sam.toposort();
while(~scanf("%s",ss))
{
for(int i=,len=strlen(ss),p=,cnt=;i<len;i++)
{
int c=ss[i]-'a';
if(sam.ch[p][c])
p=sam.ch[p][c],cnt++;
else
{
while(p&&!sam.ch[p][c]) p=sam.fa[p];
if(!p)
p=,cnt=;
else
cnt=sam.len[p]+,p=sam.ch[p][c];
}
mx[p]=max(mx[p],cnt);
}
for(int i=sam.tot;i;i--)
{
int p=sam.tp[i];
mi[p]=min(mx[p],mi[p]);
if(mx[p]&&sam.fa[p]) mx[sam.fa[p]]=sam.len[sam.fa[p]];
mx[p]=;
}
}
for(int i=;i<=sam.tot;i++) ans=max(ans,mi[i]);
printf("%d\n",ans);
return ;
}

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

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

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

  2. SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串

    [传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...

  3. SPOJ LCS2 - Longest Common Substring II

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

  4. 【SP1812】LCS2 - Longest Common Substring II

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

  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 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

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

  7. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

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

  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. [转]Linux动态库的种种要点

    linux下使用动态库,基本用起来还是很容易.但如果我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地运行. 本篇先谈谈动态库符号方面的问题. 测试代码 ...

  2. Jhipster token签名异常——c.f.o.cac.security.jwt.TokenProvider : Invalid JWT signature.

    背景,jHipster自动生成的springBoot和angularJs前后台端分离的项目.java后台为了取到当前登录者的信息,所以后台开放了 MicroserviceSecurityConfigu ...

  3. mac os x 记录 转载

    转载:远景网友(手机锋友t5sd3sf):http://bbs.feng.com/read-htm-tid-10434256.html 一个命令制作 OS X 原版安装U盘 1.要保证下载的原版安装包 ...

  4. 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于AD)

    一.进管理中心,创建一个应用程序,配置如下: 二.填端口号,和选择form身份认证,以及填写成员和角色,其他都默认就可以了 三.使用SharePoint 2010 Management Shell在里 ...

  5. 160331、使用@Controller注解为什么要配置<mvc:annotation-driven />

    为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404.直到添加了<mvc:annotation ...

  6. debian卸载旧内核

    debian卸载旧内核要先看看有哪些旧的内核,用命令: uname -a dpkg --get-selections |grep linux 如果你的内核是以kernel开头的就把上面的linux改成 ...

  7. 查看嵌入式设备的CPU频率

    对于有多个核心的CPU,查看CPU 频率的方法是: cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 上面的这个是查看核心0的cpu的 ...

  8. swift中类与结构体

    写了12个Person来复习,不过完成同样的代码需要敲键盘的次数相比OC确实少了很多,这很多应该归功于Swift中不写分号,以及少了OC中的中括号. 一.类与结构体 两者在Swift中差不了多少了 类 ...

  9. netty 网关 flume 提交数据 去除透明 批处理 批提交 cat head tail 结合 管道显示行号

    D:\javaNettyAction\NettyA\src\main\java\com\test\HexDumpProxy.java package com.test; import io.netty ...

  10. H5应用程序缓存 - Cache manifest

    一.作用 离线浏览 - 根据文件规则把资源缓存在本地,脱机依然能够访问资源,联网会直接使用缓存在本地的文件.优化加载速度,节约服务器资源. 二.适用场景 正如 manifest 英译的名字:离线应用程 ...