POJ 3415:Common Substrings 后缀数组+单调栈

来源:互联网 发布:大淘客cms文章系统 编辑:程序博客网 时间:2024/04/27 19:02

Common Substrings
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 8958 Accepted: 2994

Description

A substring of a string T is defined as:

T(ik)=TiTi+1...Ti+k-1, 1≤ii+k-1≤|T|.

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

S = {(ijk) | kKA(ik)=B(jk)}.

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

2aababaaabaabaa1xxxx0

Sample Output

225

题意是给了两个字符串A与B,求A与B的公共子串中,长度不少于k的有多少个。

刚刚看了几位神牛的博客,看他们的博文,好多名词不知道。。。被爆的渣都不剩。还是慢慢思考慢慢来吧,这东西本身真的挺有乐趣的。

最近一直在看罗神牛的后缀数组那篇论文。做到了这个题,真的真的没什么思路。。。


自己唯一的想法还是把B弄到A后面,然后求height数组,利用height数组来求结果。单调栈的使用是可以理解的,但是问题就在于单个元素与之前元素的贡献如何计算 。

后来看了别人的代码,可以计算当前元素对于结果的贡献,然后在进栈 往外面出元素的时候,将栈顶元素的贡献替换成当前元素与之前元素的贡献就可以了。


代码:

#pragma warning(disable:4996)  #include <iostream>  #include <algorithm>#include <cstring>#include <cstring>#include <vector>  #include <string>  #include <cmath>#include <queue>#include <map>using namespace std;#define INF 0x3ffffffftypedef long long ll;const int mod = 1e9 + 7;const int maxn = 200005;int K;char A[maxn], B[maxn];int sa[maxn], num[maxn];int ran[maxn], height[maxn];int wa[maxn], wb[maxn], wv[maxn], wss[maxn];struct no{ll len;ll num;no() {};no(ll x, ll y) { len = x; num = y; }}sta[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 = 0; i < m; i++){wss[i] = 0;}for (i = 0; i < n; i++){wss[x[i] = r[i]] ++;}for (i = 1; i < m; i++){wss[i] += wss[i - 1];}for (i = n - 1; i >= 0; i--){sa[--wss[x[i]]] = i;}for (j = 1, p = 1; p < n; j *= 2, m = p){for (p = 0, 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 < n; i++){wv[i] = x[y[i]];}for (i = 0; i < m; i++){wss[i] = 0;}for (i = 0; i < n; i++){wss[wv[i]] ++;}for (i = 1; i < m; i++){wss[i] += wss[i - 1];}for (i = n - 1; i >= 0; i--){sa[--wss[wv[i]]] = y[i];}for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++){x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;}}}void calHeight(int *r, int n)    {int i, j, k = 0;for (i = 1; i <= n; i++){ran[sa[i]] = i;}for (i = 0; i < n; i++){if (k){k--;}else{k = 0;}j = sa[ran[i] - 1];while (r[i + k] == r[j + k]){k++;}height[ran[i]] = k;}}ll cal(char * s){int i;int len = strlen(s);for (i = 0; i < len; i++){num[i] = s[i];}num[i] = 0;da(num, len + 1, 130);calHeight(num, len);ll ans, t, x, top;no p;t = 0;x = 0;top = -1;ans = 0;for (i = 1; i <= len; i++){if (height[i] < K){t = 0;top = -1;continue;}x = 1;//x标记这个元素之前 有多少个符合要求的元素个数t += height[i] - K + 1;//t表示当前节点产生的贡献while (top >= 0 && height[i] <= sta[top].len){p = sta[top--];x += p.num;t -= p.num*(p.len - K + 1);//这两行代码的作用 相当于把栈顶元素产生的贡献 换成 当前元素产生的贡献t += p.num*(height[i] - K + 1);}sta[++top] = no(height[i], x);ans += t;}return ans;}void solve(){ll resA, resB, res_sum;scanf("%s", A);scanf("%s", B);resA = cal(A);resB = cal(B);char *s = strcat(strcat(A, "&"), B);res_sum = cal(s);printf("%lld\n", res_sum - resA - resB);}int main(){//freopen("i.txt", "r", stdin);//freopen("o.txt", "w", stdout);while (scanf("%d", &K) != EOF){if (K == 0)break;solve();}return 0;}


0 0
原创粉丝点击