[HDU1403]Longest Common Substring(后缀数组)
求两个串的公共子串(注意,这个公共子串是连续的一段)
把两个串连在一起,中间再加上一个原字符串中不存在的字符,避免过度匹配。
求一遍height,再从height中找满足条件的最大值即可。
为什么只需要相邻两字典序的后缀呢?因为字典序相邻公共前缀一定最大。
——代码
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 200005
#define max(x, y) ((x) > (y) ? (x) : (y)) int len, n, ans, m = 'z' + ;
int buc[N], x[N], y[N], sa[N], rank[N], height[N];
char s[N]; inline void build_sa()
{
int i, k, p;
for(i = ; i < m; i++) buc[i] = ;
for(i = ; i < len; i++) buc[x[i] = s[i]]++;
for(i = ; i < m; i++) buc[i] += buc[i - ];
for(i = len - ; i >= ; i--) sa[--buc[x[i]]] = i;
for(k = ; k <= len; k <<= )
{
p = ;
for(i = len - ; i >= len - k; i--) y[p++] = i;
for(i = ; i < len; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; i++) buc[i] = ;
for(i = ; i < len; i++) buc[x[y[i]]]++;
for(i = ; i < m; i++) buc[i] += buc[i - ];
for(i = len - ; i >= ; i--) sa[--buc[x[y[i]]]] = y[i];
std::swap(x, y);
p = , x[sa[]] = ;
for(i = ; i < len; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + k] == y[sa[i] + k] ? p - : p++;
if(p >= len) break;
m = p;
}
} inline void build_height()
{
int i, j, k = ;
for(i = ; i < len; i++) rank[sa[i]] = i;
for(i = ; i < len; i++)
{
if(!rank[i]) continue;
if(k) k--;
j = sa[rank[i] - ];
while(s[i + k] == s[j + k] && i + k < len && j + k < len) k++;
height[rank[i]] = k;
}
} int main()
{
int i;
while(~scanf("%s", s))
{
n = strlen(s);
s[n] = '#';
scanf("%s", s + n + );
len = strlen(s);
build_sa();
build_height();
ans = ;
for(i = ; i < len; i++)
if((sa[i - ] < n && sa[i] > n) || (sa[i - ] > n && sa[i] < n))
ans = max(ans, height[i]);
printf("%d\n", ans);
}
return ;
}
[HDU1403]Longest Common Substring(后缀数组)的更多相关文章
- hdu 1403 Longest Common Substring 后缀数组 模板题
		
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
 - spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
		
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^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 ...
 - hdu1403 Longest Common Substring
		
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1403 题目: Longest Common Substring Time Limit: 800 ...
 - hdu_1403_Longest Common Substring(后缀数组的应用)
		
题目链接:hdu_1403_Longest Common Substring 题意: 给你两个字符串,然你找最长的公共子串 题解: 后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的 ...
 - 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 题意就是求两个字符串 ...
 - [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
		
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
 - HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
		
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
 
随机推荐
- [BZOJ 3132] 上帝造题的七分钟
			
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3132 [算法] 二维树状数组 [代码] #include<bits/stdc+ ...
 - poj3463 Sightseeing——次短路计数
			
题目:http://poj.org/problem?id=3463 次短路计数问题,在更新最短路的同时分类成比最短路短.长于最短路而短于次短路.比次短路长三种情况讨论一下,更新次短路: 然而其实不必被 ...
 - residual sum of squares(ESL 读书笔记)
			
The learning algorithm has the property that it can modify its input/output relationship f-hat in re ...
 - 70.资金管理-福利表管理 Extjs 页面
			
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
 - docker部署gitlab服务
			
docker run -d -p : -p : -p : \ --name gitlab --restart always \ --volume /home/xxx/dockerData/gitlab ...
 - [Apple开发者帐户帮助]九、参考(1)证书类型
			
该证书类型有助于开发者帐户和Xcode的标识证书. 类型 目的 APNs Auth Key 生成服务器端令牌,以替代通知请求的证书. Apple推送服务 在通知服务和APN之间建立连接,以向您的应用提 ...
 - ACM_Alien And Password
			
Alien And Password Time Limit: 2000/1000ms (Java/Others) Problem Description: Alien Fred wants to de ...
 - asp.net MVC 给Controler传一个JSon集合,后台通过List<Model>接收
			
需求情景 View层经常需要通过Ajax像后台发送一个json对象的集合,但是在后台通过List<Model>无法接收,最后只能通过妥协的方式,在后台获取一个json的字符串,然后通过Js ...
 - StackOverflowError&OutOfMemoryError区别
			
在Java虚拟机规范中,针对内存分配规定两种异常状况,即StackOverflowError和OutOfMemoryError. StackOverflowError:当线程请求的内存大小大于所配置的 ...
 - 初学者Android studio安装
			
学习过java基础,最近趁着大量课余时间想学习Android开发.百度很多资料Android studio,由Google开发的开发工具,那就不需要再多说.对于初学者的我来说,一定足够用了.此文主要介 ...