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

Common Substrings
Time Limit: 5000MS Memory Limit: 65536K
Total 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

2
aababaa
abaabaa
1
xx
xx
0

Sample Output

22
5

题意是给了两个字符串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 0x3fffffff
typedef 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值