HDU-1403-Longest Common Substring(后缀数组的高度数组运用)
这题要求两个串中的最长相同子串的长度。高度数组可以求一个串中的最长相同子串的长度。所以想到把两个串连起来,但是这样又会产生一些新的串(第一个串的结尾和第二个串的开头组成的)于是在两个串中间放一个'\0'分隔,正好'\0'是字符里最小的,不会对第一个串的排序产生影响。
| Accepted | 1403 | 62MS | 5344K | 2117 B | G++ |
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5 + ;
char s[MAXN];
int x[MAXN], y[MAXN], cnt[MAXN];
int sa[MAXN], rk[MAXN], height[MAXN];
int a, b, ans;
void getSa(int n, int m) {
for (int i = ; i <= m; i++) {
cnt[i] = ;
}
for (int i = ; i <= n; i++) {
cnt[x[i] = s[i]]++;
}
for (int i = ; i <= m; i++) {
cnt[i] += cnt[i - ];
}
for (int i = n; i; i--) {
sa[cnt[x[i]]--] = i;
}
int num = ;
for (int j = ; num < n; j <<= , m = num) {
num = ;
for (int i = n - j + ; i <= n; i++) {
y[++num] = i;
}
for (int i = ; i <= n; i++) {
if (sa[i] > j) {
y[++num] = sa[i] - j;
}
}
for (int i = ; i <= m; i++) {
cnt[i] = ;
}
for (int i = ; i <= n; i++) {
cnt[x[i]]++;
}
for (int i = ; i <= m; i++) {
cnt[i] += cnt[i - ];
}
for (int i = n; i; i--) {
sa[cnt[x[y[i]]]--] = y[i];
}
swap(x, y);
num = ;
x[sa[]] = ;
for (int i = ; i <= n; i++) {
if (y[sa[i]] != y[sa[i - ]] || y[sa[i] + j] != y[sa[i - ] + j]) {
x[sa[i]] = ++num;
} else {
x[sa[i]] = num;
}
}
}
}
void getHeight(int n) {
for (int i = ; i <= n; i++) {
rk[sa[i]] = i;
}
int k = ;
for (int i = ; i <= n; i++) {
int j = sa[rk[i] - ];
k = k ? k - : k;
while (s[i + k] == s[j + k]) {
k++;
}
// height[rk[i]] = k; // 如果i和j位于前后两个不同的串,更新ans
if ((i <= a + ) ^ (j <= a + )) {
ans = max(ans, k);
}
}
}
int main() {
while (~scanf("%s", s + )) {
a = strlen(s + );
// 把第二个字符串直接输入到第一个字符串的'\0'后面,例如输入两个"abc"就是"\0abc\0abc\0"
scanf("%s", s + a + );
b = strlen(s + a + );
// n表示第一个串的长度,m表示第二个串的长度,加上中间的'\0'要求后缀数组的部分长度为n + m + 1,也就是"abc\0abc"这部分
getSa(a + b + , );
ans = ;
// 在求高度数组的时候为避免第二个串后面的'\0'和第一个串后面的'\0'对结果造成影响。"abc\0" == "abc\0" 将第二个串后面的'\0'改成字符串中不可能出现的字符
s[a + b + ] = '';
getHeight(a + b + );
printf("%d\n", ans);
}
return ;
}
HDU-1403-Longest Common Substring(后缀数组的高度数组运用)的更多相关文章
- hdu 1403 Longest Common Substring 后缀数组 模板题
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
- hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- HDU - 1403 - Longest Common Substring
先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...
- HDU 1403 Longest Common Substring(最长公共子串)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...
- POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203
后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就 ...
- 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 ...
- 【HDOJ】1403 Longest Common Substring
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...
随机推荐
- Object中有哪些公共方法及作用
大家在学习java的时候,一定遇到过Object类,因为在java单一继承体系中Object类是根类,所有的类都会继承它,并拥有Object的公共方法,意味着在java的面向对象的世界中,所有对象都拥 ...
- PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]
题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...
- OracleXE 11g user莫名过期
参考大大的 环境sqlplus 1.sysdba登陆 SQL>conn sys as sysdba password 2.查看用户状态 SQL>select username,accoun ...
- 2019牛客暑期多校训练营(第五场)B.generator 1
传送门:https://ac.nowcoder.com/acm/contest/885/B 题意:给出,由公式 求出 思路:没学过矩阵快速幂.题解说是矩阵快速幂,之后就学了一遍.(可以先去学一下矩阵快 ...
- Python—守护进程管理工具(Supervisor)
一.前言简介 1.Supervisor 是一个 Python 开发的 client/server 系统,可以管理和监控类 UNIX 操作系统上面的进程.可以很方便的用来启动.重启.关闭进程(不仅仅是 ...
- unless|until|LABEL|{}|last|next|redo| || |//|i++|++i
#!/usr/bin/perl use strict; use warnings; $_ = 'oireqo````'; unless($_ =~ /^a/m){print "no matc ...
- oracle安装和使用
1.0 安装 2.0 初始化 1.使用 sqlplus 连接oracle数据库 1)在cmd中输入sqlplus /nolog 2)使用管理员账户登录orcl数据库实例 conn sys/gzsx ...
- D - Association for Control Over Minds Kattis - control (并查集+STL)
You are the boss of ACM (Association for Control over Minds), an upstanding company with a single go ...
- Descriptive Measures for Populations|Parameter|Statistic|standardized variable|z-score
3.4 Descriptive Measures for Populations; Use of Samples For a particular variable on a particular p ...
- 浅谈Java中的泛型
泛型是Java自JDK5开始支持的新特性,主要用来保证类型安全.另外泛型也让代码含义更加明确清晰,增加了代码的可读性. 泛型的声明和使用 在类声明时在类名后面声明泛型,比如MyList<T> ...