题目分析:

用没出现过的字符搞拼接。搞出right树,找right集合的最小和最大。如果最小和最大分居两侧可以更新答案。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; int son[maxn][],fa[maxn],maxlen[maxn],root,num,sigma = ;
int minn[maxn],maxx[maxn];// lright rright string str,ss;
int n,m;
vector<int> T[maxn]; int addnew(int dt){maxlen[++num] = dt;return num;}
int ins(char x,int p,int hhhh){
int np = addnew(maxlen[p]+); minn[np] = maxx[np] = hhhh;
while(p && !son[p][x-'a']) son[p][x-'a'] = np,p = fa[p];
if(!p){fa[np] = root; return np;}
else{
int q = son[p][x-'a'];
if(maxlen[q]==maxlen[p]+){fa[np]=q;return np;}
else{
int nq = addnew(maxlen[p]+);
for(int i=;i<sigma;i++) son[nq][i] = son[q][i];
fa[nq] = fa[q]; fa[q] = fa[np] = nq;
while(p && son[p][x-'a'] == q) son[p][x-'a'] = nq,p = fa[p];
return np;
}
}
} void dfs(int now){
if(!minn[now]) minn[now] = 1e9;
for(int i=;i<T[now].size();i++){
dfs(T[now][i]);
minn[now] = min(minn[now],minn[T[now][i]]);
maxx[now] = max(maxx[now],maxx[T[now][i]]);
}
} void work(){
int lst = addnew();root = lst;
for(int i=;i<str.length();i++)
lst = ins(str[i],lst,i+);
for(int i=;i<=num;i++) T[fa[i]].push_back(i);
dfs(); int ans = ;
for(int i=;i<=num;i++)if(minn[i]<=n&&maxx[i]>n+)ans=max(ans,maxlen[i]);
cout<<ans<<endl;
} int main(){
ios::sync_with_stdio(false);
cin.tie();
cin >> str; cin >> ss;
n = str.length(); m = ss.length();
str += ('z'+); str += ss;
work();
return ;
}

SPOJ-LCS Longest Common Substring 【后缀自动机】的更多相关文章

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

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

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

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

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

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

  4. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

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

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

  6. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  7. SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  8. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

  9. SPOJ LCS - Longest Common Substring 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...

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

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

随机推荐

  1. JS的MD5加密

    /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...

  2. 【笔记】两个根因分析方法:5WHY&10WHY

    什么是问题根因分析 根本原因分析(root cause analysis):通过调查和分析问题哪里出错.为什么出错,寻求防止差错事故再次发生的必要措施,从而提高服务安全和质量. 根因分析目标 问题(发 ...

  3. MongoDB 最大连接数 设置失效的异常分析

    背景介绍: 查询MongoDB配置参数,可以知道关于最大连接数的参数是maxConns.但是连接实例后,查看支持的最大连接数,还是默认的819. 说明:最大连接数是由maxConn (maxIncom ...

  4. C#微信支付对接

    c#版在pc端发起微信扫码支付   主要代码: /** * 生成直接支付url,支付url有效期为2小时,模式二 * @param productId 商品ID * @return 模式二URL */ ...

  5. iOS中Realm数据库的基本用法

      原文  http://git.devzeng.com/blog/simple-usage-of-realm-in-ios.html 主题 RealmiOS开发 Realm是由 Y Combinat ...

  6. mysql删除表中重复数据,只保留一个最小的id的记录

    语句: delete from table1 where id not in (select minid from (select min(id) as minid from table1 group ...

  7. 图像分析函数:skimage.measure中的label、regionprops

    算法解释详细,有算法执行过程动态GIF图的:https://blog.csdn.net/icvpr/article/details/10259577 算法文字解释的简介易懂的:https://www. ...

  8. SQLServer之修改PRIMARY KEY

    使用SSMS数据库管理工具修改PRIMARY KEY 1.连接数据库,选择数据表->右键点击->选择设计(或者展开键,选择要修改的键,右键点击,选择修改,后面步骤相同). 2.选择要修改的 ...

  9. Jquery自动补全插件的使用

    1.引入css和js  <script src="js/jquery-ui.min.js"></script> <link href="cs ...

  10. Python爬虫【解析库之beautifulsoup】

    解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...