POJ3415 Common Substrings
后缀数组 求长度不小于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的更多相关文章
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- POJ3415 Common Substrings(后缀数组 单调栈)
借用罗穗骞论文中的讲解: 计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来.先将两个字符串连起来,中间用一个没有出现过的字符隔开.按height ...
- poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)
[题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...
- 2018.12.15 poj3415 Common Substrings(后缀自动机)
传送门 后缀自动机基础题. 给两个字符串,让你求长度不小于kkk的公共子串的数量. 这题可以用后缀自动机解决废话 考虑对其中一个字串建出后缀自动机,然后用另一个在上面跑,注意到如果一个状态有贡献的话, ...
- POJ3415 Common Substrings 【后缀数组 + 单调栈】
常见的子串 时间限制: 5000MS 内存限制: 65536K 提交总数: 11942 接受: 4051 描述 字符串T的子字符串被定义为: Ť(我,ķ)= Ť 我 Ť 我 1 ... Ť I ...
- poj3415 Common Substrings (后缀数组+单调队列)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9414 Accepted: 3123 Description A sub ...
- 【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≤ ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- 字符串(后缀数组):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≤ ...
随机推荐
- Detecting diabetic retinopathy in eye images
Detecting diabetic retinopathy in eye images The past almost four months I have been competing in a ...
- 寒假222_codeforces 290 div 2 D
序号5: 想了很久的DP ,应该很简单,但是.. 题目直接转化为求n个数中选一些数GCD=1且花费最小 数比较大 map HASH 还有一点 我们知道 GCD(X,X*Y)==X; 所以我的代码里不 ...
- 使用jQuery动态加载js脚本
动态加载Javascript是一项非常强大且有用的技术.这方面的主题在本站已经讨论了不少,我也经常会在一些个人项目上使用RequireJS和Dojo加载js.它们很强大,但有时候也会得不偿失.如果你使 ...
- Tomcat自动启动脚本
Tomcat自动启动脚本#!/bin/bash # chkconfig: 2345 10 90 # description: Starts and Stops the Tomcat daemon. T ...
- spring security 构造函数初始化bean思路
采用有参数的构造方法来解决注入你要的属性例如:public MyInvocationSecurityMetadataSource(RoleService roleService) { this.rol ...
- leetcode course shedule
题目就不说了,问题本质就是在一个有向图中查找它是不是存在环. 上网百度了一下,方法是,找出图中入度为0 的点,将以它为起点的边去掉. 重复这一动作,直到所有的边都被去掉(没有环)或者存在边但是无法再去 ...
- POJ 2533 Longest Ordered Subsequence
题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...
- VS2010 Notes
1.fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK112 ...
- Spark源码分析(二)-SparkContext创建
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3872785.html SparkContext是应用启动时创建的Spark上下文对象,是一个重要的入口 ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-07View层
1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...