[AHOI2013]差异 后缀数组+并查集

版权声明:xgc原创文章,未经允许不得转载。 https://blog.csdn.net/xgc_woker/article/details/82783453

Description
给一个长度为n的字符串,Ti表示i为后缀的字符串,求:
1 < = j < i < = n l e n ( T i ) + l e n ( T j ) 2 L C P ( T i , T j ) \sum_{1<=j<i<=n}len(Ti)+len(Tj)-2*LCP(Ti,Tj)


Sample Input
cacao


Sample Output
54


后缀数组+并查集套路题,没什么好说的。。。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int _min(int x, int y) {return x < y ? x : y;}
typedef unsigned long long ULL;
typedef long long LL;
const ULL P = 131;

struct node {
	int x, id;
} height[510000];
LL f[510000];
char ss[510000];
ULL s[510000], o[510000];
int n, m, a[510000], Rank[510000], Rsort[510000], sa[510000], yy[510000];
int fa[510000], tot[510000];

bool cmp(node a, node b) {return a.x > b.x;}

int findfa(int x) {
	if(fa[x] != x) fa[x] = findfa(fa[x]);
	return fa[x];
}

void get_sa() {
	memcpy(Rank, a, sizeof(Rank));
	for(int i = 1; i <= n; i++) Rsort[Rank[i]]++;
	for(int i = 1; i <= m; i++) Rsort[i] += Rsort[i - 1];
	for(int i = n; i >= 1; i--) sa[Rsort[Rank[i]]--] = i;
	int ln = 1, p;
	while(p < n) {
		int k = 0;
		for(int i = n - ln + 1; i <= n; i++) yy[++k] = i;
		for(int i = 1; i <= n; i++) if(sa[i] - ln > 0) yy[++k] = sa[i] - ln;
		memset(Rsort, 0, sizeof(Rsort));
		for(int i = 1; i <= n; i++) Rsort[Rank[i]]++;
		for(int i = 1; i <= m; i++) Rsort[i] += Rsort[i - 1];
		for(int i = n; i >= 1; i--) sa[Rsort[Rank[yy[i]]]--] = yy[i];
		for(int i = 1; i <= n; i++) yy[i] = Rank[i];
		p = 1; Rank[sa[1]] = 1;
		for(int i = 2; i <= n; i++) {
			if(yy[sa[i]] != yy[sa[i - 1]] || yy[sa[i] + ln] != yy[sa[i - 1] + ln]) p++;
			Rank[sa[i]] = p;
		} m = p, ln *= 2;
	}
}

LL sum(int x) {return (LL)x * (x - 1) / 2;}

int main() {
	scanf("%s", ss + 1);
	n = strlen(ss + 1);
	for(int i = 1; i <= n; i++) a[i] = ss[i] - 'a' + 1;
	o[0] = 1; for(int i = 1; i <= n; i++) o[i] = o[i - 1] * P, s[i] = s[i - 1] * P + a[i];
	m = 26; get_sa();
	LL ans = (LL)(n + 1) * n / 2 * (n - 1);
	for(int i = 1; i < n; i++) {
		int x = sa[i], y = sa[i + 1];
		int l = 1, r = _min(n - x + 1, n - y + 1), ans = 0;
		while(l <= r) {
			int mid = (l + r) / 2;
			if(s[x + mid - 1] - s[x - 1] * o[mid] == s[y + mid - 1] - s[y - 1] * o[mid]) l = mid + 1, ans = mid;
			else r = mid - 1;
		} height[i].x = ans, height[i].id =i; 
	} sort(height + 1, height + n, cmp);
	for(int i = 1; i <= n; i++) fa[i] = i, tot[i] = 1;
	LL hh = 0, u = height[1].x;
	for(int i = 1; i < n; i++) {
		if(height[i].x != height[i - 1].x && i != 1) f[height[i - 1].x] = hh;
		if(height[i].x == 0) break;
		int x = sa[height[i].id], y = sa[height[i].id + 1];
		int fx = findfa(x), fy = findfa(y);
		hh -= sum(tot[fx]), hh -= sum(tot[fy]);
		tot[fy] += tot[fx], fa[fx] = fy;
		hh += sum(tot[fy]);
	} if(height[n - 1].x) f[height[n - 1].x] = hh;
	for(int i = 1; i <= u; i++) {
		f[i] -= f[i + 1];
		ans -= f[i] * i * 2;
	} printf("%lld\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xgc_woker/article/details/82783453