Description

A substring of a string T is defined as:

Tik)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|.

Given two strings AB and one integer K, we define S, a set of triples (ijk):

S = {( ijk) | k≥ KAik)= Bjk)}.

You are to give the value of |S| for specific AB and K.

Input

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 ≤ |A|, |B| ≤ 105

1 ≤ K ≤ min{|A|, |B|}

Characters of A and B are all Latin letters.

Output

For each case, output an integer |S|.

Sample Input

2
aababaa
abaabaa
1
xx
xx
0

题意:求长度不小于 k 的公共子串的个数。

思路:还是论文上的题目。基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度。把最长公共前缀长度不小于 k 的部分所有加起来。先将两个字符串连起来,中间用一个没有出现过的字符隔开。按 height 值分组后,接下来的工作便是高速的统计每组中后缀之间的最长公共前缀之和。扫描一遍,每遇到一个 B 的后缀就统计与前面的 A 的后缀能产生多少个长度不小于 k 的公共子串,这里 A 的后缀须要用一个单调的栈来高效的维护。然后对 A 也这样做一次。

比較难理解的是单调栈这部分。还是通过举例来说吧,如果当前的height[]数组按排名的顺序依次是:1,2,3.如果这些都大于等于k值,且sa[0],sa[1],sa[2]都是B串的,当sa[3]是A串的时候,由于它和sa[2]的最长公共前缀是3,所以能够包括住前3个B串,所以能够所有累加起来;可是如果是小于等于的话,比如是1的话,那么2和3的值就须要又一次计算了,由于此时的最长公共前缀是1了,我们还须要一个num[]数组来记录此时大于等于栈顶的值的个数,由于这在之后如果有更小的时候。还须要把这些大于等于的再减掉。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
typedef long long ll;
using namespace std;
const int maxn = 200010; int sa[maxn];
int t1[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn]; void build_sa(int s[], int n, int m) {
int i, j, p, *x = t1, *y = t2;
for (i = 0; i < m; i++) c[i] = 0;
for (i = 0; i < n; i++) c[x[i] = s[i]]++;
for (i = 1; i < m; i++) c[i] += c[i-1];
for (i = n-1; i >= 0; i--) sa[--c[x[i]]] = i; for (j = 1; j <= n; j <<= 1) {
p = 0;
for (i = n-j; i < n; i++) y[p++] = i;
for (i = 0; i < n; i++)
if (sa[i] >= j)
y[p++] = sa[i] - j;
for (i = 0; i < m; i++) c[i] = 0;
for (i = 0; i < n; i++) c[x[y[i]]]++;
for (i = 1; i < m; i++) c[i] += c[i-1];
for (i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = 1, x[sa[0]] = 0;
for (i = 1; i < n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+j] == y[sa[i]+j] ? p-1 : p++; if (p >= n) break;
m = p;
}
} void getHeight(int s[],int n) {
int i, j, k = 0;
for (i = 0; i <= n; i++)
rank[sa[i]] = i; for (i = 0; i < n; i++) {
if (k) k--;
j = sa[rank[i]-1];
while (s[i+k] == s[j+k]) k++;
height[rank[i]] = k;
}
} int r[maxn];
int st[maxn], num[maxn];
char str1[maxn], str2[maxn];
int k, len1, len2; ll solve(int n, int k) {
ll i, j, tp, ans = 0;
ll tot, top;
for (i = 1; i <= n; i++) {
if (height[i] < k) tot = top = 0;
else {
tp = 0;
if (sa[i-1] > len1) {
tp = 1;
tot += height[i] - k + 1;
} while (top > 0 && st[top] >= height[i]) {
tot -= num[top] * (st[top] - height[i]);
tp += num[top];
top--;
} st[++top] = height[i];
num[top] = tp;
if (sa[i] < len1)
ans += tot;
}
} for (i = 1; i <= n; i++) {
if (height[i] < k) tot = top = 0;
else {
tp = 0;
if (sa[i-1] < len1) {
tp = 1;
tot += height[i] - k + 1;
} while (top > 0 && st[top] >= height[i]) {
tot -= num[top] * (st[top] - height[i]);
tp += num[top];
top--;
} st[++top] = height[i];
num[top] = tp;
if (sa[i] > len1)
ans += tot;
}
}
return ans;
} int main() {
int i, j;
while (scanf("%d", &k) != EOF && k) {
scanf("%s%s",str1,str2);
for (i = 0; str1[i]; ++i)
r[i] = str1[i];
r[i] = '$',len1 = i,i++;
for (j = 0; str2[j];j++)
r[i+j] = str2[j];
r[i+j] = 0;
int len = i + j; build_sa(r, len+1, 128);
getHeight(r, len); printf("%lld\n", solve(len, k));
}
return 0;
}

POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)的更多相关文章

  1. POJ 3415 Common Substrings(长度不小于K的公共子串的个数+后缀数组+height数组分组思想+单调栈)

    http://poj.org/problem?id=3415 题意:求长度不小于K的公共子串的个数. 思路:好题!!!拉丁字母让我Wa了好久!!单调栈又让我理解了好久!!太弱啊!! 最简单的就是暴力枚 ...

  2. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 379 ...

  3. POJ 3415 Common Substrings 【长度不小于 K 的公共子串的个数】

    传送门:http://poj.org/problem?id=3415 题意:给定两个串,求长度不小于 k 的公共子串的个数 解题思路: 常用技巧,通过在中间添加特殊标记符连接两个串,把两个串的问题转换 ...

  4. Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

    题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1  sa[i] < len1  和  sa[i-1] < ...

  5. POJ - 3415 Common Substrings (后缀数组)

    A substring of a string T is defined as: T( i, k)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|. Given two s ...

  6. 【POJ 3415】Common Substrings 长度不小于k的公共子串的个数

    长度不小于k的公共子串的个数,论文里有题解,卡了一上午,因为sum没开long long!!! 没开long long毁一生again--- 以后应该早看POJ里的Discuss啊QAQ #inclu ...

  7. POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)

    题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...

  8. POJ 3415 Common Substrings ——后缀数组

    [题目分析] 判断有多少个长度不小于k的相同子串的数目. N^2显然是可以做到的. 其实可以维护一个关于height的单调栈,统计一下贡献,就可以了. 其实还是挺难写的OTZ. [代码] #inclu ...

  9. poj 3415 Common Substrings —— 后缀数组+单调栈

    题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...

随机推荐

  1. jq滚动条美化

    https://github.com/inuyaksa/jquery.nicescroll(插件地址) https://blog.csdn.net/zyy_0725/article/details/8 ...

  2. dedecmsV5.7自定义图片字段调用方法

    正常情况下,在列表页(也就是 {dede:list}标签)调用附加的图片类型字段则会出现Fatal error: Call to a member function GetInnerText() on ...

  3. PHP SOAP 教程

    一.SoapServer 服务器 1.__construct 作用:创建 SoapServer 对象 用法:__construct ( mixed wsdl [, array options] ) 参 ...

  4. idea 包的显示方式

    idea 可以通过点击Project的导航栏里的小齿轮里面有一个 Flatten packages 选项,将其勾上.就可以得到跟eclipse一样的包的显示方式. 没有设置默认是这样的 2018-06 ...

  5. (数据结构整理)NJUPT1054

    这一篇博客以一些OJ上的题目为载体,整理一下数据结构.会陆续的更新. .. 我们都知道,数据结构的灵活应用有时能让简化一些题目的解答. 一.栈的应用 1.NJUPT OJ 1054(回文串的推断) 回 ...

  6. shell学习-while

    1.shell while语句语法 while condition do statements done 2.shell while语句实例 #! /bin/sh 2 var1=1 3 while(( ...

  7. zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)

    1775: 和尚特烦恼1--是不是素数 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 563  Solved: 193 SubmitStatusWeb ...

  8. A string is a sequence

    A string is a sequence of characters. You can access the characters one at a time with the bracket o ...

  9. POJ 3667 线段树+标记

    自从某次考试写线段树写挂了以后 这是第一次写线段树,,,,,, 这是一个伤心的故事-- 题意: 思路: 标记 维护从左到右的最大值 从右到左的最大值 区间内的最大值-- 然后就一搞 就出来了 //By ...

  10. Android框架-Volley(一)

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...