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

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. mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost'

    排错,首先在sql编辑工具 通过测试连接,查看一下自己的密码是否正确 : 由此可见,是自己的密码错误,输入正确的密码后 在这里把密码修改为正确之后程序运行正常 #初始化数据库连接 engine = c ...

  2. Python笔记(十三)_os模块和os.path模块

    os模块中关于文件/目录常用的函数使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('.'表示当前目录,' ...

  3. fatal error C1047: The object or library file xxx was created with an older compiler than other objects

    之前编译Cocos2DX时遇到过一次,这次又遇到了,记下解决方法,以防再来第三次. 这个错误是说链接的库或者文件是老版本编译器生成的,与当前编译器不符,比如这次用的预编译库是2015的,而我当前使用的 ...

  4. Netty核心组件介绍及手写简易版Tomcat

    Netty是什么: 异步事件驱动框架,用于快速开发高i性能服务端和客户端 封装了JDK底层BIO和NIO模型,提供高度可用的API 自带编码解码器解决拆包粘包问题,用户只用关心业务逻辑 精心设计的Re ...

  5. spring-第五篇之spring容器中的bean

    1.bean的基本定义和bean别名 2.容器中bean的作用域 singleton:单例模式,在整个spring IoC容器中,singleton作用域的bean将只生成一个实例. prototyp ...

  6. dp(最长升序列)

    http://poj.org/problem?id=2533   题意:给你n(1-1000)个数,求这n个数的最长升序列.   题解:dp[i]表示以第i个数结尾的最长升序列. #include & ...

  7. [POJ3612] Telephone Wire(暴力dp+剪枝)

    [POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...

  8. P1613跑路

    题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的空间跑路器,每秒钟 ...

  9. Linux服务器安全配置小结(转)

    众所周知,网络安全是一个非常重要的课题,而服务器是网络安全中最关键的环节.Linux被认为是一个比较安全的Internet服务器,作为一种开放源代码操作系统,一旦Linux系统中发现有安全漏洞,Int ...

  10. JS方法使用中文出参数 ,报错异常

    正常这样加载数字没问题,但是当参数是中文时就会报错 <li onclick='onSeach(‘’" + name+ "');'>" + name+ &quo ...