后缀数组 求长度不小于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. 数据导出为excel表格

    ---恢复内容开始--- 方式一: 通过request和response中携带的数据导出表格,导出的结果会将页面中展示的内容全部导出.代码如下: //调出保存框,下载页面所有内容 String fil ...

  2. apple wwdc resource

    1. every wwdc topic list http://asciiwwdc.com 2. wwdc video直接查看apple develop-> video 3. wwdc open ...

  3. sql server 数据页缓冲区的内存瓶颈分析

        查看数据库的计数器: SELECT * FROM  sys.dm_os_performance_counters   **也可以使用系统的性能计监测器查看. 右键图表-> 添加计数器. ...

  4. .Net 执行 Oracle SQL语句时, 中文变问号

      带中文的Sql语句在.Net调用时, 中文变问号(可使用 SQL Tracker工具跟踪)   问题:       服务器的字符集与客户端的字符集不一致. 解决方法: 1.  查看服务端的字符集: ...

  5. Spring中自动装配(转)

    Spring中有四种自动装配类型,分别为:byName,byType,constructor,autodetect,下面来分别介绍一下这些是如何自动装配的 <bean id="foo& ...

  6. 如何将控制台程序包装成windows服务

    1. 新建一个项目,或者从选择当前解决方案--右键-添加--新建项目 2. 选择(项目类型)Visual C#项目,(模板)Windows 服务,填写要创建的服务名称(修改默认的WindowServi ...

  7. Css选择器的优先级

    a = 行内样式style. b = ID选择器的数量. c = 类.伪类和属性选择器的数量. d = 类型选择器和伪元素选择器的数量. 选择器 等级(a,b,c,d) style=”” 1,0,0, ...

  8. href=#与href=javascriptvoid(0)的区别

    #"包含了一个位置信息 默认的锚点是#top 也就是网页的上端 而javascript:void(0)  仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首 而 ...

  9. C#&java重学笔记(函数)

    C#部分  1.写在函数定义的()中的关键字: a.params关键字:用来满足函数的参数为数组时,而数组的长度不固定的情况.且该关键字只能用来修饰数组型参数.这样一修饰,就达成了类似JavaScri ...

  10. React.js 样式组件:React Style

    点这里 React Style 是 React.js 可维护的样式组件.使用 React Native StyleSheet.create一样的样式. 完全使用 JavaScript 定义样式: ? ...