POJ 3415 后缀数组
题目链接:http://poj.org/problem?id=3415
题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数。
思路:首先是两个字符串的问题。所以想用一个'#'把两个字符串拼接起来。求后缀数组。 然后按照k把height数组分组。大于等于k的为一组,然后就是统计每组的贡献。对于每一组的贡献即是组内所有A串的后缀和B串的后缀的lcp值,即为val.那么val对于答案的贡献为(val-k+1)。如果我们暴力每组的AB串后缀的组合。时间复杂度是O(n^2).不能满足要求。所以要用另外的办法来优化计算。利用height数组的性质,满足单调不增的特点,所以我们可以用一个单调栈来优化计算。 对于每个分组。我们分2种情况来计算。一:把B串的后缀放入单调栈。每枚举到一个A串后缀就和单调栈的值进行计算贡献。二:把A串的后缀放入单调栈。每枚举到一个B串后缀就和单调栈的值进行计算贡献。 对于如何维护这个单调栈:如果对于情况一,如果每遇到一个A串后缀就和栈内所有的B后缀都计算一遍会退化到O(n^2).所以栈还要维护一个前缀贡献值,这样就可以把复杂度将为O(n)。 关于维护前缀贡献值是从这篇博客学习到的。 该题也可以用后缀自动机来写,而且好像比后缀数组容易。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
#include<stack>
using namespace std;
typedef long long int LL;
const int MAXN = * + ;
int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
void da(int *r, int *sa, int n, int m){
int i, j, p, *x = wa, *y = wb, *t;
for (i = ; i < m; i++) { WS[i] = ; }
for (i = ; i < n; i++) { WS[x[i] = r[i]]++; }
for (i = ; i < m; i++) { WS[i] += WS[i - ]; }
for (i = n - ; i >= ; i--) { sa[--WS[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++) { WS[i] = ; }
for (i = ; i < n; i++) { WS[wv[i]]++; }
for (i = ; i < m; i++) { WS[i] += WS[i - ]; }
for (i = n - ; i >= ; i--) { sa[--WS[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++;
}
}
return;
}
int Rank[MAXN], height[MAXN], sa[MAXN];
void calheight(int *r, int *sa, 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++);
}
return;
}
int r[MAXN], len, k, index;
char str[MAXN];
struct Node{
int cnt, height; LL sum;
//cnt:后缀个数; height:sa[i]和sa[i-1]的lcp;sum:前缀贡献和
}S[MAXN];
void solve(){
LL ans = ; int top=;
for (int i = ; i <= len; i++){ //计算A串的贡献
if (height[i] < k) top = ;
else {
int cnt = ;
while (top && height[i] <= S[top - ].height) cnt += S[--top].cnt; //单调栈
S[top].cnt = cnt + (sa[i - ] > index); //属于B串
S[top].height = height[i]; //记录height
S[top].sum = top ? S[top - ].sum : ; //累加之前的[前缀]贡献
S[top].sum += (LL)(S[top].height - k + ) * S[top].cnt; //计算当前贡献
if (sa[i] < index) ans += S[top].sum; //属于A串,统计贡献
top++;
}
}
top = ;
for (int i = ; i <= len; i++){ //与上面相反
if (height[i] < k) top = ;
else {
int cnt = ;
while (top && height[i] <= S[top - ].height) cnt += S[--top].cnt;
S[top].cnt = cnt + (sa[i - ] < index);
S[top].height = height[i];
S[top].sum = top ? S[top - ].sum : ;
S[top].sum += (LL)(S[top].height - k + ) * S[top].cnt;
if (sa[i] > index) ans += S[top].sum;
top++;
}
}
printf("%lld\n", ans);
}
int main(){
while (scanf("%d", &k) && k){
scanf("%s", str); index = strlen(str); str[index] = '#';
scanf("%s", str + index + ); len = strlen(str); str[len] = '$';
for (int i = ; i <= len; i++){
r[i] = str[i];
}
r[index] = ; r[len] = ;
da(r, sa, len+, );
calheight(r, sa, len);
solve();
}
return ;
}
POJ 3415 后缀数组的更多相关文章
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- POJ 3415 后缀数组+单调栈
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...
- poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数
Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11469 Accepted: 379 ...
- poj 3693 后缀数组 重复次数最多的连续重复子串
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8669 Acc ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
- POJ 1226 后缀数组
题目链接:http://poj.org/problem?id=1226 题意:给定n个字符串[只含大小写字母],求一个字符串要求在n个串或者他们翻转后的串的出现过.输出满足要求的字符串的长度 思路:根 ...
- POJ 3294 后缀数组
题目链接:http://poj.org/problem?id=3294 题意:给定n个字符串,求一个最长子串要求在超过一半的字符串中出现过. 如果多解按字典序输出 思路:根据<<后缀数组— ...
- POJ 2774 后缀数组
题目链接:http://poj.org/problem?id=2774 题意:给定两个只含小写字母的字符串,求字符串的最长公共子串长度. 思路:根据<<后缀数组——处理字符串的有力工具&g ...
- POJ 3693 后缀数组
题目链接:http://poj.org/problem?id=3693 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现在给定一个长度为n的 ...
随机推荐
- 检索COM 类工厂中 CLSID 为 {} 的组件时失败
- 【HTTP】Wireshark过滤规则
参考:http://jingyan.baidu.com/article/454316ab593170f7a6c03a60.html 语句特点:协议.属性 一.IP过滤: 包括来源IP或者目标IP等于某 ...
- 如何手动添加Windows服务和如何把一个服务删除
windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...
- mysql探究之null与not null
相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...
- osgearth使用prjected投影
In projected mode, you have to specify a map profile (i.e. a map projection). You also need to tell ...
- Undefined symbols for architecture x86_64: ( linker command failed with exit code 1)
当出现 linker command failed with exit code 1 (use -v to see invocation) 的错误总结,具体内容如下: Undefined symbo ...
- [Android Pro] Android libdvm.so 与 libart.so
reference to :http://blog.csdn.net/koffuxu/article/details/44780351 Android libdvm.so 与 libart.so ...
- [Android Pro] How to get recent tasks on Android “L”?
reference : http://stackoverflow.com/questions/24590533/how-to-get-recent-tasks-on-android-l/2688546 ...
- August 8th 2016, Week 33rd Monday
Everything is going on, but don't give up trying. 万事随缘,但不要放弃努力. Every time when I want to give up, y ...
- Angular.JS
AngularJS是什么? 完全使用 JavaScript编写的客户端技术.同其他历史悠久的 Web技术( HTML. CSS 和JavaScript)配合使用,使Web应用开发比以往更简单.更快捷. ...