题意:一个字符串,查询恰好出现k次的子串的数目
思路:后缀数组在height上进行操作。我们直接枚举长度为k的区间求min值,但是要注意的是直接这么算是会重复的,同时也可能超过k次,这样我们就需要把枚举的前一个和后一个都判断一下,这样保证了等于k次,同时也保证了没有重复统计。
代码:
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define MEM(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXN = 5e5+100;
int t1[MAXN], t2[MAXN], c[MAXN],n;
bool cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int str[], int sa[], int zrank[], int height[], int n, int m){
n++;
int i, j, p, *x = t1, *y = t2;
//第一轮基数排序,如果s的最大值很大,可改为快速排序
for (i = 0; i < m; i++)c[i] = 0;
for (i = 0; i < n; i++)c[x[i] = str[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 (j = 1; j <= n; j <<= 1){
p = 0;
//直接利用sa数组排序第二关键字
for (i = n - j; i < n; i++)y[p++] = i;//后面的j个数第二关键字为空的最小
for (i = 0; i < n; i++)if (sa[i] >= j)y[p++] = sa[i] - j;
//这样数组y保存的就是按照第二关键字排序的结果
//基数排序第一关键字
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];
//根据sa和x数组计算新的x数组
swap(x, y);
p = 1; x[sa[0]] = 0;
for (i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
if (p >= n)break;
m = p;//下次基数排序的最大值
}
int k = 0;
n--;
for (i = 0; i <= n; i++)zrank[sa[i]] = i;
for (i = 0; i < n; i++){
if (k)k--;
j = sa[zrank[i] - 1];
while (str[i + k] == str[j + k])k++;
height[zrank[i]] = k;
}
}
int m_rank[MAXN], height[MAXN];
int RMQ[MAXN];
int mm[MAXN];
int best[30][MAXN];
void initRMQ(int n){
mm[0] = -1;
for (int i = 1; i <= n; i++)
mm[i] = ((i&(i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1];
for (int i = 1; i <= n; i++)best[0][i] = i;
for (int i = 1; i <= mm[n]; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++){
int a = best[i - 1][j];
int b = best[i - 1][j + (1 << (i - 1))];
if (RMQ[a]<RMQ[b])best[i][j] = a;
else best[i][j] = b;
}
}
int askRMQ(int a, int b){
int t;
t = mm[b - a + 1];
b -= (1 << t) - 1;
a = best[t][a]; b = best[t][b];
return RMQ[a]<RMQ[b] ? a : b;
}
char str[MAXN];
int r[MAXN];
int sa[MAXN];
int Q[MAXN];
int lcp(int a, int b){
//a = m_rank[a]; b = m_rank[b];
if(a==b) return n-sa[a];
if (a>b)swap(a, b);
return height[askRMQ(a + 1, b)];
}
int main() {
int t, k;
scanf("%d", &t);
while (t--) {
scanf("%d", &k);
scanf("%s", str);
n = strlen(str);
for (int i = 0; i < n; i++)r[i] = str[i];
r[n] = 0;
da(r, sa, m_rank, height, n, 256);
long long ans = 0;
for (int i = 1; i <= n; i++) {
RMQ[i] = height[i];
}
initRMQ(n);
memset(Q, 0, sizeof(Q));
for (int i = 0; i+k<=n; i++) {
Q[i+1] = lcp(i+k,i);
}
int ret;
ans=0;
for (int i = 0; i+k-1 <=n; i++) {
ret=lcp(i+k-1,i);
ans = ans+(long long)max(0,ret-max(Q[i], Q[i+1]));
}
printf("%lld
", ans);
}
return 0;
}