SPOJ 1812 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
題解:
這題走了許多彎路,最後好不容易拍完錯誤,代碼沒有改得精簡.
思路可以參考:
根據上題可以想到,拿每一個串都在A得SAM上跑,然後記錄當前串在SAM得每一個節點的最大值,然後如果是多個串的話
直接記錄最小值即可,最後掃一邊記錄最小值的最大值即可
注意一些細節:
對於沒一個串的匹配,如果當前結點被匹配到了,那麼他的父親結點都可以匹配到,那麼直接拓撲序更新即可,不然就WA#10哦..
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define il inline
#define RG register
using namespace std;
const int N=;
char s[N];int cur=,cnt=,last=,fa[N],ch[N][],dis[N],n=;
void build(int c)
{
last=cur;cur=++cnt;
RG int p=last;dis[cur]=++n;
for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
if(!p)fa[cur]=;
else{
int q=ch[p][c];
if(dis[q]==dis[p]+)fa[cur]=q;
else{
int nt=++cnt;dis[nt]=dis[p]+;
memcpy(ch[nt],ch[q],sizeof(ch[q]));
fa[nt]=fa[q];fa[q]=fa[cur]=nt;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
}
}
}
int id=,t[N],tot[N],mx[N],sa[N];bool mark[N][];
void FLr()
{
RG int p=,l=strlen(s),c,u,len=;id++;
for(RG int i=;i<l;i++){
c=s[i]-'a';
u=ch[p][c];
if(u){
mark[u][id]=true;len++;
p=u;
}
else{
while(p && !ch[p][c])p=fa[p];
if(p){
mark[ch[p][c]][id]=true;
len=dis[p]+;
p=ch[p][c];
}
else p=,len=;
}
if(len>mx[p])mx[p]=len;
}
for(RG int i=cnt;i;i--){
p=sa[i];
if(mx[p]<tot[p])tot[p]=mx[p];
if(fa[p] && mark[p][id])mx[fa[p]]=dis[fa[p]];
mx[p]=;
}
}
int c[N];
void Dfp(){
RG int p,i,j;
for(i=cnt;i;i--){
p=sa[i];
for(j=;j<=id;j++)
mark[fa[p]][j]|=mark[p][j];
}
for(i=;i<=cnt;i++)
for(j=;j<=id;j++)
if(mark[i][j])t[i]++;
}
int ans=;
void dfs(){
for(int i=;i<=cnt;i++){
if(t[i] && tot[i]>ans)ans=tot[i];
}
}
void jip(){
RG int i;
for(i=;i<=cnt;i++)c[dis[i]]++;
for(i=;i<=n;i++)c[i]+=c[i-];
for(i=cnt;i;i--)sa[c[dis[i]]--]=i;
}
void work()
{
scanf("%s",s);
for(RG int i=,l=strlen(s);i<l;i++)build(s[i]-'a');
for(RG int i=;i<=cnt;i++)tot[i]=N;
jip();
while(~scanf("%s",s))FLr();
Dfp();dfs();
printf("%d\n",ans);
}
int main()
{
freopen("pow.in","r",stdin);
freopen("pow.out","w",stdout);
work();
return ;
}
SPOJ 1812 Longest Common Substring II的更多相关文章
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)
[题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 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. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- Virtual Judge SPOJ - LCS2 Longest Common Substring II
https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...
随机推荐
- 如何减小ios安装包大小
以前的老文章了,搬到cnblog 更小的安装包意味着更快的下载安装速度,也往往意味着更快的加载运行速度,是优化ios应用的一个重要方面,本文主要参考<减小iOS应用程序的大小>,在实际测试 ...
- Python choice() 函数
Python choice() 函数 Python 数字 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choice() 方法的语法: import random ...
- 利用flask 实现简单模版站
from flask import Flask,render_template from flask import request app = Flask(__name__) @app.route(' ...
- IOS UITextView自适应高度
LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...
- github提交代码到服务器的方法
第一种情况,没有冲突:1.git add .//进入到center的项目下将本地文件打包的意思2.git pull origin dev//将服务器的代码下载到本地如果是最新的会提示Already u ...
- 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
依赖注入在 ASP.NET Core 中起中很重要的作用,也是一种高大上的编程思想,它的总体原则就是:俺要啥,你就给俺送啥过来.服务类型的实例转由容器自动管理,无需我们在代码中显式处理. 因此,有了依 ...
- sts 和 lombok
1.安装lombok.jar到sts.exe所在目录 如果是eclipse,需要放到eclipse.exe所在目录,同理myeclipse. 2.修改sts.ini配置使用lombok 如果是ecli ...
- ELK学习总结(2-5)elk的版本控制
----------------------------------------------------------------- 1.悲观锁和乐观锁 悲观锁:假定会发生并发冲突,屏蔽一切可能违反数据 ...
- 【52ABP实战教程】00-- ASP.NET CORE系列介绍
为什么是.net core? 记得在半年前.NET CORE刚刚出了1.0,当时有朋友推荐我使用的时候,个人觉得还不成熟. 现在.NET Core已经到了2.0,.NET Standard 2.0 添 ...
- C#微信公众号——本地调试
测试微信,因为要与微信服务器进行交互,所以必须要是外网地址,实现本地调试首先需要解决的问题就是外网问题,这个我前面的文章有介绍,这里就不再详细介绍了,网址http://www.cnblogs.com/ ...