地址:http://www.spoj.com/problems/LCS/

题面:

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 思路:
  对于串a建立后缀自动机, 然后让串b在sam上能走,记录到达每个状态时的长度,取个max就行。
 #include <bits/stdc++.h>

 using namespace std;

 struct SAM
{
static const int MAXN = 3e5 * ;//大小为字符串长度两倍
static const int LetterSize = ;
int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN]; void init( void)
{
last = tot = ;
len[] = ;
memset(ch,,sizeof ch);
memset(fa,,sizeof fa);
} void add( int x)
{
int p = last, np = last = ++tot;
len[np] = len[p] + ;
while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if( p == )
fa[np] = ;
else
{
int q = ch[p][x];
if( len[q] == len[p] + )
fa[np] = q;
else
{
int nq = ++tot;
memcpy( ch[nq], ch[q], sizeof ch[q]);
len[nq] = len[p] + ;
fa[nq] = fa[q], fa[q] = fa[np] = nq;
while( p && ch[p][x] == q)
ch[p][x] = nq, p = fa[p];
}
}
}
};
char sa[],sb[];
SAM sam;
int main(void)
{
scanf("%s%s",sa,sb);
int len=strlen(sa),ans=;
sam.init();
for(int i=;i<len;i++)
sam.add(sa[i]-'a');
len=strlen(sb);
for(int i=,p=,cnt=;i<len;i++)
{
int k=sb[i]-'a';
if(sam.ch[p][k])
p=sam.ch[p][k],cnt++;
else
{
while(p&&!sam.ch[p][k]) p=sam.fa[p];
if(p==)
p=,cnt=;
else
cnt=sam.len[p]+,p=sam.ch[p][k];
}
ans=max(ans,cnt);
}
printf("%d\n",ans);
return ;
}

spoj1811 LCS - Longest Common Substring的更多相关文章

  1. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  2. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  3. 【SP1811】LCS - Longest Common Substring

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

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

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

  5. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

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

  6. SPOJ 1811 LCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...

  7. 【刷题】SPOJ 1811 LCS - Longest Common Substring

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

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

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

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

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

随机推荐

  1. WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...

  2. SQL的学习

    1. 判断表达式的值是否为空在表达式后面接 IS NULL 或 IS NOT NULL 可以判断表达式是否为空或不为空 2. 把数据库中的数据导出成可执行的SQL语句对数据库点击右键一次选择 任务-- ...

  3. LAMP环境搭建博客

    背景: 公司要用到lamp环境,让我装,我就开始着手找资料,一般分为源码装和yum装,源码装很容易出错,所以我选择了yum装,. 服务器:aliyun服务器  centos6.8系统 按照第一个安装完 ...

  4. Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 解决办法

    相信很多人都遇到过这个问题,用Android Studio正在运行程序的时候,突然不知道什么原因,报一个找不到application或者找不到activity的错误(java.lang.ClassNo ...

  5. 延迟加载JavaScript

    上代码: 这段代码放到HTML文档的</body>标签之前(靠近HTML文档底部).外部脚本的名称为defer.js. <script type="text/javascr ...

  6. asp 中创建日志打印文件夹

    string FilePath = HttpRuntime.BinDirectory.ToString(); string FileName = FilePath + "日志" + ...

  7. 170420、maven内置常量

    Maven工程插件配置中通常会用到一些Maven变量,因此需要找个地方对这些变量进行统一定义,下面介绍如何定义自定义变量. 在根节点project下增加properties节点,所有自定义变量均可以定 ...

  8. [ Office 365 开发系列 ] 身份认证

    前言 本文完全原创,转载请说明出处,希望对大家有用. 通常我们在开发一个应用时,需要考虑用户身份认证及授权,Office 365使用AAD(Azure Active Directory)作为其认证机构 ...

  9. SharePoint PerformancePoint开发实例

    前言 由于工作的原因,有一段时间没有发新的随笔了,最近使用了SharePoint PerformancePoint做了一些报表,与大家分享经验. 本文完全原创,转载请说明出处,希望对大家有用. 阅读目 ...

  10. C# DataTable和DataRelation

    form2.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...