[NOI2016]优秀的拆分 [后缀数组+ST表]

\(\sum_{i=1}^{n-1} f_i * g_(i+1)\)
\(f_i\) 表示 \(i\) 结尾会有多少个 \(AA\)
\(g_i\) 表示 \(i\) 开头会有多少个 \(BB\)
后缀数组求一下就可以了QAQ

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
  FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
  Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
  }FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
  ~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
  FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
  FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
  Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
		do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
  }FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;
#define int long long

const int maxn = 3e4 + 43 ;
char a[maxn] ;
int n , lg[maxn] , f[maxn] , g[maxn] ;
struct Suffix_Array {
	int sa[maxn] , rk[maxn] , lcp[maxn] ;
	void buildSA() { static int x[maxn] , y[maxn] , c[maxn] ;
#define clr(x) memset(x , 0 , sizeof(x))
		clr(sa) , clr(rk) , clr(lcp) , clr(x) , clr(y) , clr(c) ;
#undef clr
		int M = 122 ; rep(i , 1 , n) c[x[i] = a[i]] ++ ; 
		rep(i , 1 , M) c[i] += c[i - 1] ; Rep(i , n , 1) sa[c[x[i]] --] = i ;
		for(int k = 1 ; k <= n ; k <<= 1) {
			int p = 0 ;
			rep(i , 0 , M) y[i] = 0 ; rep(i , n - k + 1 , n) y[++ p] = i ;
			rep(i , 1 , n) if(sa[i] > k) y[++ p] = sa[i] - k ;
			rep(i , 0 , M) c[i] = 0 ; rep(i , 1 , n) c[x[y[i]]] ++ ;
			rep(i , 1 , M) c[i] += c[i - 1] ; Rep(i , n , 1) sa[c[x[y[i]]] --] = y[i] ;
			swap(x , y) ; x[sa[1]] = p = 1 ;
			rep(i , 2 , n) x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? p : ++ p ;
			if(p >= n) break ; M = p ;
		} rep(i , 1 , n) rk[sa[i]] = i ;
		for(int i = 1 , j = 0 ; i <= n ; i ++) {
			if(j) j -- ; while(a[i + j] == a[sa[rk[i] - 1] + j]) ++ j ;
			lcp[rk[i]] = j ;
		}
	}
	int st[maxn][16] ;
	void buildST() {
		memset(st , 63 , sizeof(st)) ; rep(i , 1 , n) st[i][0] = lcp[i] ;
		rep(j , 1 , 15) rep(i , 1 , n) st[i][j] = min(st[i][j - 1] , st[i + (1 << j - 1)][j - 1]) ;
	}
	int qry(int l , int r) {
		const int L = l , R = r ; l = min(rk[L] , rk[R]) + 1 , r = max(rk[L] , rk[R]) ;
		int t = lg[r - l + 1] ; return min(st[l][t] , st[r - (1 << t) + 1][t]) ;
	}
} A , B ;
signed main() {
  // code begin.
  rep(i , 2 , maxn - 5) lg[i] = lg[i >> 1] + 1 ;
	int T ; scanf("%lld" , & T) ; while(T --) {
		scanf("%s" , a + 1) , n = strlen(a + 1) ;
	  A.buildSA() , A.buildST() ; reverse(a + 1 , a + n + 1) ; B.buildSA() , B.buildST() ;
	  memset(f , 0 , sizeof(f)) , memset(g , 0 , sizeof(g)) ;
	  for(int len = 1 ; len <= (n >> 1) ; len ++) {
	  	for(int i = len , j = i + len ; j <= n ; i += len , j += len) {
	  		int lcp = min(A.qry(i , j) , len) , lcs = min(B.qry(n - i + 2 , n - j + 2) , len - 1) ;
	  		int t = lcp + lcs - len + 1 ;
	  		if(lcp + lcs >= len) { g[i - lcs] ++ , g[i - lcs + t] -- ; f[j + lcp - t] ++ , f[j + lcp] -- ; }
			}
		} rep(i , 1 , n) f[i] += f[i - 1] ; rep(i , 1 , n) g[i] += g[i - 1] ;
		int ans = 0 ; rep(i , 1 , n - 1) ans += f[i] * g[i + 1] ;
		out << ans << '\n' ;
	}
	return 0;
  // code end.
}
posted @ 2020-02-29 22:50  _Isaunoya  阅读(121)  评论(0编辑  收藏  举报