SPOJ 1812 Longest Common Substring II

时间:2021-07-19 08:58:42

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的更多相关文章

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

  2. SPOJ 1812 Longest Common Substring II(后缀自动机)

    [题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...

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

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

  4. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  5. SPOJ LCS2 - Longest Common Substring II

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

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

  7. 【SPOJ】1812&period; Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

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

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

  9. Virtual Judge SPOJ - LCS2 Longest Common Substring II

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

随机推荐

  1. android自定义控件一站式入门

    自定义控件 Android系统提供了一系列UI相关的类来帮助我们构造app的界面,以及完成交互的处理. 一般的,所有可以在窗口中被展示的UI对象类型,最终都是继承自View的类,这包括展示最终内容的非 ...

  2. 【转】TensorFlow练习20&colon; 使用深度学习破解字符验证码

    验证码是根据随机字符生成一幅图片,然后在图片中加入干扰象素,用户必须手动填入,防止有人利用机器人自动批量注册.灌水.发垃圾广告等等 . 验证码的作用是验证用户是真人还是机器人:设计理念是对人友好,对机 ...

  3. Spring如何处理线程并发

    Spring如何处理线程并发   我们知道Spring通过各种DAO模板类降低了开发者使用各种数据持久技术的难度.这些模板类都是线程安全的,也就是说,多个DAO可以复用同一个模板实例而不会发生冲突.我 ...

  4. mysql隔离机制

    转  MySQL隔离级别 mysql-Innodb事务隔离级别-repeatable read详解(转)

  5. Delphi PChar与String互转

    1.String转化成PChar 例: var str: string; pStr:PChar; ... pStr := PChar(str); 2.PChar转String 例: var pStr: ...

  6. Struts2&lpar;四&rpar;——页面相关内容

    上篇博客总结了数据流转各个方面的内容,这篇重点说一下框架对于界面上知识. 一,说到页面,记得在总体介绍中,说到Struts2比Struts1的一方面优势就是它支持更多的视图技术(Freemarker, ...

  7. windows之实现3D立体效果的三种方法

    第一种:快捷键:win+tab 另外一种:cmd输入rundll32.exe dwmapi #105 第三种:使用软件bumptop

  8. JVM 运行时数据区总结 栈 堆 堆大小配置总结

    1. 程序计数器 线程私有 当前线程所执行的字节码的行号指示器 2. 虚拟机栈 线程私有 存:Java方法(局部变量表(基本数据类型).操作数栈.动态链栈.方法出口) *Err ...

  9. 1&period;Python爬虫入门一之综述

    要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...

  10. 线程简述(Thread&rpar;

    线程: 进程是一个正在运行的程序,例如电脑上现在在运行的qq,浏览器,电脑管家,这些都是进程 线程就是每一个进程中的一个执行单元,每一个进程至少一个线程,可以有多个线程,例如浏览器上每一个打开的网页都 ...