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 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
随机推荐
- 数据库存入数据后id保持不变,或者直接报错
数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 或者直接报错 数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 原因是: 之前注释掉了loadimage();在该函数中含有建立新的记 ...
- jdbc的简单实现demo
直接上代码吧,只是因为上篇的心血来潮.总结哈 import java.sql.Connection; import java.sql.DriverManager; import java.sql.Re ...
- Gson简要使用笔记(转载)
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需 ...
- win10系统下把Oracle卸载干净
我和大家一样,遇到了一个问题,就是如何把Oracle从自己的电脑卸载干净,很多人都觉得很难把Oracle卸载干净,于是选择重装系统,因为解决不了Oracle没有卸载干净之后,重装不了的问题,有时候真的 ...
- java面试基础题------》Java 中的父子类静态代码块,代码块,构造方法执行顺序
4.指出下面程序的运行结果. class A { static { System.out.print("1"); } public A() { System.out.print(& ...
- Android sharedPreferences 用法
Android 提供了一种数据轻量级的数据持久化方法.使用SharedPreferences 接口 将 key-value 形式的primitive data 存储到文件中.多用于保存软件偏好配置信息 ...
- navicat导入数据出现中文乱码
1.问题描述: excel转为csv后,导入英文时没错.但中文就是显示的乱码. 2.解决方法: 将csv用记事本打开,调整编码格式为UTF-8, 点击保存覆盖掉原文件,然后再 导入,乱码问题就解决
- CGI/FastCGI/mod_php工作原理
先了解一下普通cgi的工作流程:web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web ser ...
- HDFS分布式集群安装
HDFS集群安装: 1.准备工作 虚拟机(电脑8G内存 磁盘500GB) 3台 linux系统(1台namenode 2台datanode) (1)关闭防火墙 firewall-cmd --state ...
- mysql覆盖索引
话说有这么一个表: CREATE TABLE `user_group` ( `id` int(11) NOT NULL auto_increment, `uid` int(11) NOT NU ...