LCS - Longest Common Substring

no tags 

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 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   这题就是求最长公共子串,于是我直接套用了SAM。
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct SAM{
int fa[MAXN],ch[MAXN][],len[MAXN],cnt,last;
void Init()
{
last=cnt=;
}
void Insert(int c)
{
int p=last,np=last=++cnt;
len[np]=len[p]+;
while(!ch[p][c]&&p)
ch[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else{
int q=ch[p][c];
if(len[q]==len[p]+)
fa[np]=q;
else{
int nq=++cnt;len[nq]=len[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[p]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
while(ch[p][c]==q)
ch[p][c]=nq,p=fa[p];
}
}
}
}sam;
char str1[MAXN],str2[MAXN];
int main()
{
int len1=,len2=,ans=;
scanf("%s%s",str1,str2);
sam.Init();
while(str1[len1])
sam.Insert(str1[len1++]-'`');
int node=,tmp=;
for(;str2[len2];len2++){
int c=str2[len2]-'`';
if(sam.ch[node][c])
node=sam.ch[node][c],tmp++;
else{
while(node&&!sam.ch[node][c])
node=sam.fa[node];
if(node==)
node=,tmp=;
else
tmp=sam.len[node]+,node=sam.ch[node][c];
}
ans=max(ans,tmp);
}
printf("%d\n",ans);
return ;
}

后缀自动机(SAM) :SPOJ LCS - Longest Common Substring的更多相关文章

  1. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  2. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

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

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

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

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

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

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  6. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  7. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  8. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

  9. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

随机推荐

  1. Bash函数使用

    #!/bin/bash function Fun_Name() { #function here echo "this is a function" } Fun_Name

  2. rabbitmq 消息持久化之receive and send

    二: 任务分发 &消息持久化   启用多个接收端的时候如果某一个receive 关闭要保证消息有反馈是否收到   send端 #-*- coding: UTF-8 -*-import pika ...

  3. Python开发【第二十篇】:缓存

    Python开发[第二十篇]:缓存redis&Memcache   点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...

  4. 表达式:使用API创建表达式树(4)DynamicExpression

    DynamicExpression:表示动态操作.这个网上可见的资料少得可怜,但想到MVC和第三方的动态语言能在NET运行.好奇的倒腾了下 先声明两个类(有相同的方法和字段,但不是继承于同一接口的类) ...

  5. Android清单文件AndroidMenifest.xml

    1.AndroidMenifes.xml清单文主要结构件结构 所谓主要结构就是每一个清单文件中都必不可少的结构主要是下面三层 第一层.menifest 第二层.application,use-sdk ...

  6. 动软代码生成器三层用于winform

    DBUtility项目中的DbHelperSQL.cs (找自己对应的数据库类型) 修改前20行中的数据库连接字符串获取方式为: //数据库连接字符串(web.config来配置),多数据库可使用Db ...

  7. java异步上传图片

    第一步:引入需要的js <script src="/res/common/js/jquery.js" type="text/javascript"> ...

  8. Ojbect-C2 3、NSArray和NSMutableArray数组的使用

    Adopted Protocols NSCoding encodeWithCoder: initWithCoder: NSCopying copyWithZone: NSMutableCopying ...

  9. 绘图quartz之渐变

    实现线性渐变   径向渐变   自己新建的view中有一个drawRect:(cgrect)rect方法 在这个方法里 可以通过画图 将内容显示在画板上(即最下边的view)   渐变的方式分两种   ...

  10. 启动scala的方法

    1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...