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. IE浏览器中不支持cookie问题

    /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT ...

  2. U3D 如何计算一个UI四个角的绝对坐标

      //方式一,使用API获取 var rtrans = gameObject.GetComponent<RectTransform>(); Vector3[] worldcorners ...

  3. 【Git】四、Git工作

    一.Git创建仓库 版本库:代码仓库(repository),可以理解为一个项目的目录,在这个项目的目录中Git对每个文件进行管理,记录每个文件的增删改查记录,并能够追踪历史,在需要的时候可以回退到某 ...

  4. spring security+cas(cas proxy配置)

    什么时候会用到代理proxy模式? 举一个例子:有两个应用App1和App2,它们都是受Cas服务器保护的,即请求它们时都需要通过Cas 服务器的认证.现在需要在App1中通过Http请求访问App2 ...

  5. CWinApp: The Application Class

    [CWinApp: The Application Class]  An application built on the framework must have one and only one o ...

  6. Configure、中间件与ErrorHandlingMiddleware全局异常捕获

    一.Configure Startup.cs中的Configure方法主要是http处理管道配置.中间件和一些系统配置,其中 IApplicationBuilder: 定义一个类,该类提供配置应用程序 ...

  7. JS 语法大全

    来源http://blog.csdn.net/newegg2009/article/details/6230582 js语法2008年03月19日 星期三 11:14一.js的数据类型和变量 Java ...

  8. 命名空间p方式的属性注入

    ---------------------siwuxie095 命名空间 p 方式的属性注入 命名空间 p 方式的属性注入是 Spring 2.x 版本后提供的方式 1.编写一个普通类 Book.ja ...

  9. POJ1163 数学三角求最大路径

    描述:输入,行数,之后接数据,第一行一个数据,之后每行加一.5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 思路:简单动态规划问题.dp[i][j]定义为到这个数为止(包括这个数)的最 ...

  10. SQLServer性能优化之---水平分库扩展

      汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitia ...