Common Substrings POJ - 3415 (后缀数组 + 单调栈)
2021-04-21 03:26
标签:span out typedef 字符串连接 sub pac || tput ons A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. S = {(i, j, k) | k≥K, A(i, k)=B(j, k)}. Input 1 ≤ |A|, |B| ≤ 105 Output Sample Input 题意:求串1和串2中所有长度 \(\geq k\) 的相同字串个数。 Common Substrings POJ - 3415 (后缀数组 + 单调栈) 标签:span out typedef 字符串连接 sub pac || tput ons 原文地址:https://www.cnblogs.com/PCCCCC/p/13282920.html
Given two strings A, B and one integer K, we define S, a set of triples (i, j, k):
You are to give the value of |S| for specific A, B and K.
The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.
1 ≤ K ≤ min{|A|, |B|}
Characters of A and B are all Latin letters.
For each case, output an integer |S|.
2
aababaa
abaabaa
1
xx
xx
0
Sample Output
22
5
思路:首先要把两个字符串连接起来,并用一个没出现的字符分隔, 假设 Sa[j] 属于串1,Sa[i ~ (j - 1)] 属于串2, 且 Height[i + 1] 到 Height[j] 单调递减且 Height[j] \(\geq k\) ,
根据后缀数组的性质, Sa[i ~ (j - 1)] 和 Sa[j] 的贡献为 (j - i) * (Height[i] - k + 1), 所以我们可以维护一个单调递增的栈, 将所有连续递减的Height[i]压缩成一个块,
记录这个块的最小 Height 和属于串2的后缀的个数,当 Sa[i] 属于串1时,加上前面所有块的贡献,为了快速计算贡献和,要维护一个前缀和。
所以我们for两次, 一次求串1的后缀和该后缀前面所有串2后缀的贡献,一次求串2的后缀和该后缀前面所有串1后缀的贡献。#include
文章标题:Common Substrings POJ - 3415 (后缀数组 + 单调栈)
文章链接:http://soscw.com/index.php/essay/77417.html