[Ahoi2013]差异 bzoj3238

后缀数组+分治


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
typedef long double LF;

const int MAXN(500010);
const int MAXM(50010);
const int MAXE(150010);
const int MAXK(6);
const int HSIZE(1313);
const int SIGMA_SIZE(4);
const int MAXH(20);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(31313);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> inline void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

struct SA
{
	char S[MAXN];
	int sa[MAXN],t1[MAXN], t2[MAXN], cnt[MAXN], len, M;
	void init(int tl, int tm = 128)
	{
		len = tl;
		M = tm;
		int *p1 = t1, *p2 = t2;
		for(int i = 0; i < M; ++i) cnt[i] = 0;
		for(int i = 0; i <= len; ++i) ++cnt[p1[i] = S[i]];
		for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];
		for(int i = len; i >= 0; --i) sa[--cnt[p1[i]]] = i;
		int temp = 1;
		for(int k = 1; temp <= len; k <<= 1)
		{
			temp = 0;
			for(int i = len-k+1; i <= len; ++i) p2[temp++] = i;
			for(int i = 0; i <= len; ++i)
				if(sa[i] >= k) p2[temp++] = sa[i]-k;
			for(int i = 0; i < M; ++i) cnt[i] = 0;
			for(int i = 0; i <= len; ++i) ++cnt[p1[p2[i]]];
			for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];
			for(int i = len; i >= 0; --i) sa[--cnt[p1[p2[i]]]] = p2[i];
			swap(p1, p2);
			temp = 1;
			p1[sa[0]] = 0;
			for(int i = 1; i <= len; ++i)
				p1[sa[i]] = p2[sa[i-1]] == p2[sa[i]] && p2[sa[i-1]+k] == p2[sa[i]+k]? temp-1: temp++; 
			M = temp;
		}
	}
	int rank[MAXN], hei[MAXN]; 
	void getHei()
	{
		int k = 0;
		for(int i = 0; i <= len; ++i) rank[sa[i]] = i;
		for(int i = 0; i < len; ++i)
		{
			if(k) --k;
			int j = sa[rank[i]-1];
			while(S[i+k] == S[j+k]) ++k;
			hei[rank[i]] = k;
		}
	}
} sa;

LL tans;
int rec[MAXN];
LL sum[MAXN];

void dfs(int l, int r)
{
	if(l == r) return;
	int m = (l+r) >> 1;
	int tl = m-1, tr = m+1;
	while(tl >= l && sa.hei[tl+1] > 0) --tl;
	while(tr <= r && sa.hei[tr] > 0) ++tr;
	rec[m] = sa.hei[m+1];
	for(int i = m-1; i > tl; --i) rec[i] = min(rec[i+1], sa.hei[i+1]);
	sum[tl] = 0;
	for(int i = tl+1; i <= m; ++i) sum[i] = sum[i-1]+rec[i];
	int ti = m, th = sa.hei[m+1];
	for(int i = m+1; i < tr; ++i)
	{
		checkmin(th, sa.hei[i]);
		while(ti > tl && rec[ti] >= th) --ti;
		tans += (LL)th*(m-ti);
		tans += sum[ti];
	}
	dfs(l, m);
	dfs(m+1, r);
}

int main()
{
	while(~scanf("%s", sa.S))
	{
		sa.init(strlen(sa.S));
		sa.getHei();
		tans = 0;
		dfs(1, sa.len);
		LL len = sa.len;
		printf("%lld\n", (len+1)*len/2*(len-1)-2*tans);
	}
	return 0;
}


你可能感兴趣的:([Ahoi2013]差异 bzoj3238)