spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/
题面:
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
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的更多相关文章
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串
[传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 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 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 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, \(\ ...
- 【刷题】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 ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
随机推荐
- 【Python】用文本打印树
From:http://zhidao.baidu.com/link?url=O8U5TynGBMojDw2iFhlghPPf5_ZE1X8CAQMrK19pv-KxhvKCc6Z2yzsoQaukgN ...
- MyBitis(iBitis)系列随笔之一:MyBitis入门实例
MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助. ...
- Ubuntu 16.04 LTS 完善解决亮度调整
环境: ubuntu16.04 lts acer aspire 4752G i5-2450M 内容来源:点击这里 ubuntu无法调整屏幕亮度,对笔记本来说很耗电,同时也很刺眼,因为它是默认以最大亮度 ...
- OpenSSL创建私有CA
1.编辑/etc/pki/tls/openssl.cnf [ CA_default ] dir = /etc/pki/CA # 工作目录certs ...
- c#基础 第一讲
using System;using System.Collections.Generic; using System.Text; namespace MYTest{ class Program { ...
- 测试csscss层叠顺序
<!Doctype html><html lang="zh-CN"><head><meta charset="utf-8&quo ...
- 获取 js DOM元素中绑定的所有事件,模仿 chrome getEventListeners
偶尔看到了这个问题,如何用JS获取元素某一事件上绑定的所有Listener? 突然觉得好像是有解决办法的,查了下,在 chrome 下,支持 window.getEventListeners(obj) ...
- Strut2流程分析-----从请求到Action方法()
手写请求会通过strutsPrepareAndExcuteFliter的doFilter()方法 然后会调用StrutsActionProxy类的excute()方法,生成一个代理类(ActionPr ...
- CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CH5401 没有上司的舞会【树形DP】
5401 没有上司的舞会 0x50「动态规划」例题 描述 Ural大学有N名职员,编号为1~N.他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司.每个职员有一个快乐指数,用整数 H_i 给 ...