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

In this problem, \(\sum\) 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 simple, for two 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 exactly two lines, each line consists of no more than 250000 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

Output:

3

Notice: new testcases added

题意:

求两个字符串的最长公共子串

题解:

把第一个串建一个后缀自动机,然后把另一个串放在上面从根(1号节点)开始跑,如果失配,就往\(parent\)上跳,并把当前长度变为\(parent\)的\(len\);若匹配,把当前长度加一,并实时更新答案。

#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
char s[N];
int a[N],c[N];
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
size[np]=1;
}
void build(char s[]){
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void work(char s[]){
int len=strlen(s+1);
int p=1,left=0,as=0,ans=0;
while(left<=len){
left++;
while(p&&(!ch[p][s[left]-'a']))p=fa[p],as=l[p];
if(!p)p=1,as=0;
else{
as++;
ans=max(ans,as);
p=ch[p][s[left]-'a'];
}
}
cout<<ans<<endl;
}
}sam;
int main(){
cin>>s+1;
sam.build(s);
cin>>s+1;
sam.work(s);
}

LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)的更多相关文章

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

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

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

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

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

  4. SPOJ LCS - Longest Common Substring 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...

  5. SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解

    题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...

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

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

  7. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

  8. 【SP1811】 LCS - Longest Common Substring(SAM)

    传送门 洛谷 Solution 考虑他要求的是最长公共子串对吧,那么我们对于一个串建后缀自动机,另一个串在后缀自动机上面跑就是了. 复杂度\(O(n+m)\)的,很棒! 代码实现 代码戳这里

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

随机推荐

  1. FORALL用法小结

    本文主要翻译.整理了ORACLE官方文档上有关FORALL的部份内容,不妥之处,还希望多和大家交流. 在发送语句到SQL引擎前,FORALL语句告知PL/SQL 引擎批挷定输入集合.尽管FORALL语 ...

  2. 问题记录,StartCoroutine(“str")问题

    StartCoroutine参数为函数字符串名,运行时出错,错误是:无法启动协程函数. 调用格式如下: gameManager.StartCoroutine(LuaOnLevelwasloaded() ...

  3. 在MarkDown中插入数学公式对照表(持续更新)

    目录 在MarkDown中可以插入数学公式,但是在博客园和有道云笔记之中的数学公式插入方式略有不同(博客园需要先在后台选项中开启插入数学公式选项): 代码 行内公式 整行公式 博客园 $数学公式$ $ ...

  4. python 把txt文件分隔成0.8和0.2的比例的新文件

    from math import sqrt import randomimport osfrom sklearn import cross_validation os.chdir("/*&q ...

  5. SO_LINGER

    [SO_LINGER] 在默认情况下,当调用close关闭socket的使用,close会立即返回,但是,如果send buffer中还有数据,系统会试着先把send buffer中的数据发送出去,然 ...

  6. 【hdu3507】Print Article 【斜率优化dp】

    题意 https://cn.vjudge.net/problem/HDU-3507 分析 斜率优化的模板题 #include <cstdio> #include <cstring&g ...

  7. 监测IIS上网站的连接数

    1.运行,输入,perfmon.msc 2.性能监视器>右侧区域>右键 添加计数器 3.在“可用计数器” 区域,选择Web Service,展开Web Service,选择Current ...

  8. FineUI学习

    1.无限级菜单绑定 using (DataTable dt = SqlPagerHelper.GetTableByCondition(DefaultConnection.ConnectionStrin ...

  9. Linux gcj命令

    一.简介 GCJ是GNU的Java编译器,可以把java程序编译成本地代码,编译成功后的可执行文件不再需要jre就可直接运行,编译成本地后的程序运行速度有所提高,缺点是生成后的文件较大. 参考: ht ...

  10. windows cmake安装

    https://blog.csdn.net/u013832707/article/details/53127710