source:2017 ACM/ICPC Asia Regional Shenyang Online
题意:给你n个字符串,问恰好出现k次的字符串有几种。
用后缀数组构造出height数组后,注意到对于一段连续的长度为k的区间,前缀长度的下界为
max(h[L],h[R+1)
,上界为
min(h[L+1],h[L+2]...,h[R])
,这里对上界的求法可以用RMQ维护,也可以用一个单调递增的双端队列维护。
注意
k==1
时特判。
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define CLR(A, X) memset(A, X, sizeof(A))
#define bitcount(X) __builtin_popcountll(X)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-10;
const LL MOD = 1e9+7;
const auto INF = 0x3f3f3f3f;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }
const int MAXN = 1e5+10;
char str[MAXN];
int s[MAXN], sa[MAXN], x[MAXN], y[MAXN], c[MAXN];
int Rank[MAXN], h[MAXN];
int q[MAXN];
void get_sa(int m, int n) {
int i, k, p;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
for(k = 1, p = 1; k<=n && p<n; k <<= 1, m = p) {
for(i = n-k, p = 0; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y); p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++) {
int l = sa[i-1], r = sa[i];
if(y[l]==y[r] && y[l+k]==y[r+k]) x[sa[i]] = p-1;
else x[sa[i]] = p++;
}
}
for(i = 0; i < n; i++) Rank[sa[i]] = i;
for(i = 0, k = 0; i < n; i++) {
if(k) k--; if(!Rank[i]) continue;
int j = sa[Rank[i]-1];
while(s[j+k] == s[i+k]) k++;
h[Rank[i]] = k;
}
}
int main() {
int X, k;
scanf("%d", &X);
while(X--) {
scanf("%d%s", &k, str);
int n = strlen(str);
for(int i = 0; i < n; i++) s[i] = str[i]-'a'+1;
s[n] = h[0] = 0; h[++n] = 0;
get_sa(27, n);
int L = 0, R = -1; LL ans = 0;
if(k == 1) {
for(int i = 1; i < n; i++) ans += n-sa[i]-1-max(h[i], h[i+1]);
printf("%lld\n", ans);
continue;
}
for(int i = 2; i < n; i++) {
while(L<=R && h[q[R]]>h[i]) R--;
q[++R] = i;
while(i-q[L]+1 >= k) L++;
if(i >= k) {
int t1 = h[q[L]], t2 = max(h[i-k+1], h[i+1]);
ans += max(t1-t2, 0);
}
}
printf("%lld\n", ans);
}
return 0;
}