后缀数组 求长度不小于k的公共子串的个数

代码:

 #include <stdio.h>
#include <string.h> const int maxn = ;
int len, len1;
int wa[maxn], wb[maxn], wv[maxn], wd[maxn], sa[maxn];
int lcp[maxn], r[maxn], rank[maxn], height[maxn]; int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
} void da(int *r, int n, int m){
int i, j, p, *x=wa, *y=wb, *t;
for(i = ; i < m; i++) wd[i] = ;
for(i = ; i < n; i++) wd[x[i] = r[i]]++;
for(i = ; i < m; i++) wd[i] += wd[i-];
for(i = n-; i >= ; i--) sa[--wd[x[i]]] = i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , i = n-j; i < n; i++) y[p++] = i;
for(i = ; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(i = ; i < n; i++) wv[i] = x[y[i]];
for(i = ; i < m; i++) wd[i] = ;
for(i = ; i < n; i++) wd[wv[i]]++;
for(i = ; i < m; i++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[--wd[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i++){
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p-: p++;
}
}
} void calHeight(int *r, int n){
int i, j, k = ;
for(i = ; i <= n; i++) rank[sa[i]] = i;
for(i = ; i < n; height[rank[i++]] = k){
for(k ? k-- : , j = sa[rank[i]-]; r[i+k] == r[j+k]; k++);
}
} int main(){
int k;
char str1[maxn], str2[maxn];
while(~scanf("%d", &k)){
if(k==) break;
scanf("%s%s",str1, str2);
len1 = strlen(str1);
len = strlen(str2);
for(int i = ; i < len1; i++){
r[i] = str1[i];
}
r[len1] = '$';
for(int i = ; i < len; i++){
r[i+len1+] = str2[i];
}
len += len1+;
r[len] = ;
da(r, len+, );
calHeight(r, len);
long long res = , sum;
int head, tail;
for(int i = ; i <= len; i++){
if(height[i] < k){
sum = ;
head = tail = maxn;
}
else{
for(int j = head; j < tail; j++){
if(lcp[j] > height[i]){
sum -= lcp[j]-height[i];
lcp[j] = height[i];
}
else break;
}
if(sa[i-] > len1){
lcp[--head] = height[i];
sum += lcp[head]-k+;
}
if(sa[i] < len1) res += sum;
}
}
for(int i = ; i <= len; i++){
if(height[i] < k){
sum = ;
head = tail = maxn;
}
else{
for(int j = head; j < tail; j++){
if(lcp[j] > height[i]){
sum -= lcp[j]-height[i];
lcp[j] = height[i];
}
else break;
}
if(sa[i-] < len1){
lcp[--head] = height[i];
sum += lcp[head]-k+;
}
if(sa[i] > len1) res += sum;
}
}
printf("%I64d\n", res);
}
}

POJ3415 Common Substrings的更多相关文章

  1. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  2. POJ3415 Common Substrings(后缀数组 单调栈)

    借用罗穗骞论文中的讲解: 计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来.先将两个字符串连起来,中间用一个没有出现过的字符隔开.按height ...

  3. poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)

    [题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...

  4. 2018.12.15 poj3415 Common Substrings(后缀自动机)

    传送门 后缀自动机基础题. 给两个字符串,让你求长度不小于kkk的公共子串的数量. 这题可以用后缀自动机解决废话 考虑对其中一个字串建出后缀自动机,然后用另一个在上面跑,注意到如果一个状态有贡献的话, ...

  5. POJ3415 Common Substrings 【后缀数组 + 单调栈】

    常见的子串 时间限制: 5000MS   内存限制: 65536K 提交总数: 11942   接受: 4051 描述 字符串T的子字符串被定义为: Ť(我,ķ)= Ť 我 Ť 我 1 ... Ť I ...

  6. poj3415 Common Substrings (后缀数组+单调队列)

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9414   Accepted: 3123 Description A sub ...

  7. 【POJ3415】 Common Substrings(后缀数组|SAM)

    Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...

  8. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  9. 字符串(后缀数组):POJ 3415 Common Substrings

    Common Substrings   Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...

随机推荐

  1. 手写PE文件(二)

    [文章标题]: 纯手工编写的PE可执行程序 [文章作者]: Kinney [作者邮箱]: mohen_ng@sina.cn [下载地址]: 自己搜索下载 [使用工具]: C32 [操作平台]: win ...

  2. Keil中的code关键字

    一般说来,我们在C语言中定义的每一个变量初始化后都会占用一定的内存(RAM)空间.但是在keil中提供了一个特殊的关键字“code”,这个关键字在标准C中是没有的.其语法举例如下: unsigned ...

  3. WPF编程学习——样式

    本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...

  4. https+ssl详解

    这是转载别人的写的很好,(转:崔永秀) 把这几天学习到的关于ssl和https协议的内容在这里分享一下,适合一些像我一样的网络协议初学者. ssl协议的起源和历史我就不再多说了,就是那个Netscap ...

  5. Java读取图片并修改像素,创建图片

    public void replaceImageColor(String file, Color srcColor, Color targetColor) throws IOException{ UR ...

  6. Xml Schema的用途

    Xml Schema的用途 1.  定义一个Xml文档中都有什么元素 2.  定义一个Xml文档中都会有什么属性 3.  定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4.  ...

  7. 用 VIPER 构建 iOS 应用架构(2)

    [编者按]本篇文章由 Jeff Gilbert 和 Conrad Stoll 共同编写,通过构建一个基础示例应用,深入了解 VIPER,并从视图.交互器等多个部件理清 VIPER 的整体布局及思路.通 ...

  8. codeforces 442C C. Artem and Array(有深度的模拟)

    题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...

  9. iOS网络检测

    使用之前请从Apple网站下载示例:点此下载 Reachability 中定义了3种网络状态: typedef enum : NSInteger { NotReachable = ,//无网络 Rea ...

  10. jvm性能调优---jstat的用法

    Jstat是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JVM内建的指令 ...