spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/
题面:
LCS - Longest Common Substring
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的更多相关文章
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- SPOJ 1811 LCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...
- 【刷题】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 ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
随机推荐
- 配置gosublime
Installation Sublime Package Control allows you to easily install or remove GoSublime (and many othe ...
- python入门(七):CGI编程
CGI CGI(Common Gateway Interface),通用网关接口,HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI在其中扮演的是在we ...
- gcc安装(centos)
gcc 4.8 安装 [root@DS-VM-Node239 ~]# curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repo ...
- 自定义View总结
写的很好,代你分析原码,关于 View Measure 测量机制,让我一次把话说完
- 【BZOJ4518】[Sdoi2016]征途 斜率优化
[BZOJ4518][Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除 ...
- centos免密登录
本文是为了docker-machine增加现有虚拟机服务器为节点而做 docker-machine create -d generic --generic-ip-address=192.168.102 ...
- 160315、mybatis批量删除
<deleteid="deleteCTQ" parameterType="java.lang.String"> DELETE FROM sqm_pr ...
- 使用 paramsPrepareParamsStack 拦截器栈后的运行流程
2. 使用 paramsPrepareParamsStack 拦截器栈后的运行流程 1). paramsPrepareParamsStack 和 defaultStack 一样都是拦截器栈. 而 st ...
- kibana5.6源码分析3--目录结构
kibana5.6的项目目录结构: bin:系统启动脚本目录 config:kibana配置文件目录 data:估计是缓存一些系统数据的,uuid放在这里面 docs: maps:此目录包含TileM ...
- Django 翻译与 LANGUAGE_CODE
LANGUAGE_CODE[1] LANGUAGE_CODE 是 language code 的字符串.格式与 Accept-Language HTTP header 相同,不区分大小写,比如:zh, ...