SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)
1811. Longest Common SubstringProblem code: LCS |
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
本题要求2个字符串的LCS.
那么先将A串读入,再在A串上匹配B串显然
但是一个结点能表示的后缀有|maxS|-|minS|+1个。
那么就这样:
假设当前在结点x,下一个B串要匹配的字母为c
如果x有ch[c],走过去len+1
否则找pre,更新len=pre的maxS
理由如下:
设原有串=ababd,babd,abd ,匹配为其中某一个
不存在abdx无法匹配.
取pre=bd,d,(由定义可知不存在‘dc存在匹配,bdc不存在’的情况,故它俩能为1个状态)
那么len=pre的maxS(step)
否则返回根,len=0
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (500000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
char s1[MAXN],s2[MAXN];
struct node
{
int pre,step,ch[26];
char c;
node(){pre=step=c=0;memset(ch,sizeof(ch),0);}
}a[MAXN];
int last=0,total=0;
void insert(char c)
{
int np=++total;a[np].c=c+'a',a[np].step=a[last].step+1;
int p=last;
for(;!a[p].ch[c];p=a[p].pre) a[p].ch[c]=np;
if (a[p].ch[c]==np) a[np].pre=p;
else
{
int q=a[p].ch[c];
if (a[q].step>a[p].step+1)
{
int nq=++total;a[nq]=a[q];a[nq].step=a[p].step+1;
a[np].pre=a[q].pre=nq;
for(;a[p].ch[c]==q;p=a[p].pre) a[p].ch[c]=nq;
}else a[np].pre=q;
}
last=np;
}
int main()
{
// freopen("spojLCS.in","r",stdin);
scanf("%s%s",s1+1,s2+1);int n=strlen(s1+1),m=strlen(s2+1);
For(i,n) insert(s1[i]-'a');
int now=0,ans=0,len=0;
For(i,m)
{
char c=s2[i]-'a';
while (now&&!a[now].ch[c]) now=a[now].pre,len=a[now].step;
if (a[now].ch[c]) now=a[now].ch[c],len++;
ans=max(ans,len);
}
printf("%d\n",ans); return 0;
}
SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)的更多相关文章
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- SPOJ 1811 Longest Common Substring 后缀自动机
模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
随机推荐
- svn版本控制
svn自动生成X.java.mine,X.java.r2333,X.java.r2368文件 这是你和别人的操作形成了冲突: 你和别人都从2333这个版本对X.java进行了修改, 别人先修改之后,先 ...
- BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )
离线然后倒着做就变成了支持加点的动态凸包...用平衡树维护上凸壳...时间复杂度O(NlogN) --------------------------------------------------- ...
- Spark源码学习1
转自:http://www.cnblogs.com/hseagle/p/3664933.html 一.基本概念(Basic Concepts) RDD - resillient distributed ...
- PHP开发APP接口
第1章 APP接口简介 - 课程简介 (:) - APP接口介绍 (:) - 客户端APP通信 (:) 最近学习 - 客户端APP通信格式区别 (:) - APP接口做的哪些事儿 (:) 第2章 封装 ...
- C#学习日志 day 3 ------ 基本语句示例
写c#首先需要知道的就是数据类型,这里是所有c#中的所有数据类型以及说明.
- 【Howie玩docker】-命令行只显示-bash-4.1#
灵雀云上面用docker建了个centOS的实例,首个免费,正好当云主机来玩. 但是,打开有个问题,命令行不显示当前用户和路径. 只显示: -bash-4.1# 简单,配置文件不全而已. 下面对其重新 ...
- 终于懂了:两个UI组件同时在操作是不可能实现的
// 目的:从某个对话框里,选择一些路径,然后用Tree自动展开这些路径,但至少需要几秒钟时间 // 问题:在这几秒钟期间,显示一个等待对话框,只能开多线程,因为后台继续要处理tree的一些事情.等待 ...
- QListWidget的QComboBox下拉列表添加复选框及消息处理
要在QComboBox下拉列表项中添加复选框,并进行消息处理,在网上搜索了很久没有找到太多有用的信息和实际的例子,但从中还是找到了一些提示性的资料,根据这些简短的介绍,最终实现了这个功能. QComb ...
- openStack juno for ubuntu12-04
<一,preinstall basic conf,pre Env> 1,pwgen(openssl rand -hex 10) some Open-Stack services add a ...
- cpu亲和力总结taskset和setcpu及其他相关
一:taskset -- 获取或指定进程运行的CPU. man taskset出现 CPU affinity is a scheduler property that "bonds" ...