这题要求两个串中的最长相同子串的长度。高度数组可以求一个串中的最长相同子串的长度。所以想到把两个串连起来,但是这样又会产生一些新的串(第一个串的结尾和第二个串的开头组成的)于是在两个串中间放一个'\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(后缀数组的高度数组运用)的更多相关文章

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

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

  2. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  3. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

  4. HDU - 1403 - Longest Common Substring

    先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

  6. HDU 1403 Longest Common Substring(最长公共子串)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...

  7. POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203

    后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就 ...

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

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

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

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

  10. 【HDOJ】1403 Longest Common Substring

    后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...

随机推荐

  1. 108.生成和下载csv文件

    生成CSV文件 有时候我们做的网站,需要将一些数据,生成一个csv文件返回浏览器,并且是作为附件的形式下载下来. 生成小的csv文件: 生成一个小的csv文件,我们用Python内置的csv模块来处理 ...

  2. hdu1066 Last non-zero Digit in N!(求阶乘最后一位不为0的数字)

    http://acm.hdu.edu.cn/showproblem.php?pid=1066 转自:https://blog.csdn.net/fengyu0556/article/details/5 ...

  3. 第1章 分布式系统概念与ZooKeeper简介

    ZooKeeper分布式专题与Dubbo微服务入门 第1章 分布式系统概念与ZooKeeper简介 1-1 zookeeper简介 1-2 什么是分布式系统 略 1-3 分布式系统的瓶颈以及zk的相关 ...

  4. mongodb的常见使用命令行

    由于cms工程要连接mongodb所以需要在在cms服务端工程添加如下依赖:项目使用spring data mongodb操作mongodb数据库 <dependency> <gro ...

  5. E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)

    You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...

  6. 我是如何从Java转型为Go区块链工程师

    我是如何从Java转型为Go区块链工程师 本文来自于一个比原链核心开发的陈述 前言 IT部落在加入比原链之前一直是做Java开发的,当初看到Go还有点犹豫,还怕过不了面试,结果是否掌握一门语言的考量确 ...

  7. 884A. Book Reading#抽空学习好孩子(模拟)

    题目出处:http://codeforces.com/problemset/problem/884/A 题目大意:每天时间分两部分,工作和学习,工作优先,闲暇读书,问第几天读完 #include< ...

  8. CF 1095C Powers Of Two

    题目连接:http://codeforces.com/problemset/problem/1095/C 题目: C. Powers Of Two time limit per test 4 seco ...

  9. BTree

    hash.平衡二叉树.BTree.B+tree的区别 https://blog.csdn.net/qq_40673786/article/details/90082444 联合索引在B+树上的结构介绍 ...

  10. [原]UEFI+GPT启动VHD

    1. 缘起 2. 创建VHD文件并写入系统镜像到VHD文件 2.1 制作VHD文件 2.1.1 纯界面创建 2.1.2 命令行创建 2.2 把系统镜像写入VHD文件 3. 添加VHD文件到系统引导 3 ...