题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度

n<=250000

思路:

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,int>P;
#define N 510000
#define M 151000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const int MOD=,inv2=(MOD+)/;
double eps=1e-;
ll INF=1e18;
ll inf=5e13;
int dx[]={-,,,};
int dy[]={,,-,}; char s[N];
int n,i,x,p,q,np,nq,cnt,L,st[N],c[N][],f[N],pos[N],bl[N],to[N],b[N],sz[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void add(int x)
{
p=np;
st[np=++cnt]=st[p]+;
to[np]=i;
pos[i]=np;
for(;p&&!c[p][x];p=f[p]) c[p][x]=np;
if(!p) f[np]=;
else if(st[p]+==st[q=c[p][x]]) f[np]=q;
else
{
st[nq=++cnt]=st[p]+;
memcpy(c[nq],c[q],sizeof c[q]);
f[nq]=f[q];
f[q]=f[np]=nq;
for(;p&&c[p][x]==q;p=f[p]) c[p][x]=nq;
}
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
np=cnt=;
scanf("%s",s);
n=strlen(s);
rep(i,,n-) add(s[i]-'a'); rep(i,,cnt) b[st[i]]++;
rep(i,,n) b[i]+=b[i-];
rep(i,,cnt) bl[b[st[i]]--]=i;
for(i=,p=;i<n;i++) sz[p=c[p][s[i]-'a']]++;
for(i=cnt;i;i--) sz[f[bl[i]]]+=sz[bl[i]];
//rep(i,0,n-1) printf("%d ",sz[pos[i]]);
scanf("%s",s);
n=strlen(s);
int ans=;
for(p=,L=i=;i<n;i++)
{
if(c[p][x=s[i]-'a']) p=c[p][x],L++;
else
{
for(;!c[p][x]&&p;p=f[p]);
if(!p) p=,L=;
else L=st[p]+,p=c[p][x];
}
ans=max(ans,L);
}
printf("%d\n",ans);
return ;
}

【SPOJ1811】Longest Common Substring(后缀自动机)的更多相关文章

  1. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

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

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

  3. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

  4. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

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

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

  6. spoj1811 Longest Common Substring

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

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

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

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

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

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

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

随机推荐

  1. linux文件夹 权限为所有用户可 读写

    使用命令: sudo chmod dirname -R

  2. day09—css布局解决方案之全屏布局

    转行学开发,代码100天——2018-03-25 今天,本文记录全屏布局的的方法.全屏布局,即滚动条不是全局滚动条,而是出现在内容区域内,:浏览器变大时,撑满窗口. 如:设置下图中布局,其中top区, ...

  3. delphi之猥琐的webserver实现

    http://www.birdol.com/cainiaobiancheng/238.html delphi之猥琐的webserver实现 菜鸟编程  十五楼的鸟儿  7年前 (2009-01-01) ...

  4. WPF样式统一之DevExpress设置窗体,控件为Office风格

    DevExpress相信不少人用过,虽然人家不是免费的,但是用过的应该都知道,确实是花了心血的C#插件,下面来介绍下在DevExpress下如何统一设置自己的WPF程序为经典Windows风格. 窗体 ...

  5. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  6. 搭建用例管理平台phpstudy+testlink代替Apache+MySQL+PHP环境,以及testlink搭建环境报错修复

    公司需要搭建一个用例管理平台,环境搭建Apache+MySQL+PHP环境 哇一看就是需要花很长时间去搭建环境,本来我也在用这样的笨方法,不小心被公司开发看到,经人家一提点,哎呀妈呀发现自己以前的方法 ...

  7. python3 -tensorflow-dataset 之map函数 lambda隐函数的应用

    ###隐函数lanbda及map函数的应用实例 ''' lambda 参数1,参数2,参数3,...:表达式 ''' f = lambda x,y :x+y print(f(1,2)) ###输出3 ...

  8. [Python3] 024 面向对象 第四弹

    目录 11. 类和对象的三种方法 12. 抽象类 12.1 抽象方法 12.2 抽象类 12.3 抽象类的使用 13. 自定义类 接上一篇 [Python3] 023 面向对象 第三弹 11. 类和对 ...

  9. mybatis 批量update报语法错误解决方法

    1.为什么会报语法错误 原因:在 *.xml文件内使用了循环,在mybatis中默认是不允许使用批量修改. <update id="setMaxMin" parameterT ...

  10. 5.1properties属性

    需求: 将数据库连接参数单独配置在db.properties文件中,只需在SqlMapconfig.xml中加载db.properties的属性值. 在SqlMapconfig.xml中就不需要对数据 ...