标签:build 就是 需要 amp https ctime getchar 数据 clr
$ \text{sam}$ 裸题,但是由于 \(\text{sam}\) 不熟练,于是用 \(\text{sa}\) 做
建出 \(\text{sa}\) 之后单调队列维护连续 \(k\) 个的 \(lcp\) 长度,设为 \(len\),以及两边相邻的位置上分别扩展一个的新 \(lcp\) 长度,这个就是 \(height\) 数组中两个值,然后长度在这两个值中较大者加 \(1\) 和 \(len\) 之间的子串恰好出现 \(k\) 次,用差分记录下来,然后扫一遍取最大值即可
注意:
// Copyright: lzt
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__)
ll read(){
ll x=0,f=1;char c=getchar();
while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int maxn = 200200;
int tc, n, k;
char s[maxn];
int buc[maxn], X[maxn], Y[maxn], sa[maxn], h[maxn], rnk[maxn];
int cnt[maxn];
#define clr(a) memset(a, 0, sizeof(a))
inline void init() {
clr(buc); clr(X); clr(Y); clr(sa); clr(h); clr(rnk); clr(cnt);
}
inline void doit(int N = n) {
rep(i, 1, n) buc[i] = 0;
rep(i, 1, n) buc[X[i]]++;
rep(i, 1, N) buc[i] += buc[i - 1];
rrep(i, n, 1) {
sa[buc[X[Y[i]]]] = Y[i];
buc[X[Y[i]]]--;
}
}
inline void build_sa() {
rep(i, 1, n) X[i] = s[i], Y[i] = i;
doit(200);
for (int k = 1; k <= n; k <<= 1) {
int num = 0;
rep(i, n - k + 1, n) Y[++num] = i;
rep(i, 1, n) if (sa[i] > k) Y[++num] = sa[i] - k;
doit(n);
rep(i, 1, n) Y[i] = X[i];
num = 1; X[sa[1]] = 1;
rep(i, 2, n) {
if (Y[sa[i]] == Y[sa[i - 1]] && Y[sa[i] + k] == Y[sa[i - 1] + k]) X[sa[i]] = num;
else X[sa[i]] = ++num;
}
if (num == n) break;
}
rep(i, 1, n) rnk[sa[i]] = i;
int nw = 0;
rep(i, 1, n) {
if (rnk[i] == 1) continue;
if (nw > 0) nw--;
int j = sa[rnk[i] - 1];
while (i + nw <= n && j + nw <= n && s[i + nw] == s[j + nw]) nw++;
h[rnk[i]] = nw;
}
}
int q[maxn * 2], L, R;
inline void solve() {
n = strlen(s + 1);
build_sa();
L = 1; R = 0;
if (k == 1) q[++R] = n - sa[1] + 1;
else rep(i, 2, k) {
while (L <= R && h[i] < q[R]) R--;
q[++R] = h[i];
}
rep(i, 1, n - k + 1) {
int mn = q[L];
if (mn > 0) {
int a, b;
if (i != 1) a = h[i];
else a = 0;
if (i + k <= n) b = h[i + k];
else b = 0;
a = max(a, b) + 1;
if (a > mn) a = mn + 1;
cnt[a]++; cnt[mn + 1]--;
}
if (i != n - k + 1) {
if (k == 1) {
R = L - 1;
q[++R] = n - sa[i + 1] + 1;
} else {
int a = h[i + 1], b = h[i + k];
if (q[L] == a) L++;
while (L <= R && b < q[R]) R--;
q[++R] = b;
}
}
}
rep(i, 1, n) cnt[i] += cnt[i - 1];
int mx = 0;
rep(i, 1, n) if (cnt[i] > cnt[mx] || cnt[i] == cnt[mx] && i > mx) mx = i;
if (cnt[mx] == 0) mx = -1;
printf("%d\n", mx);
}
void work() {
scanf("%d", &tc);
while (tc--) {
scanf("%s", s + 1);
scanf("%d", &k);
init(); solve();
}
}
int main(){
#ifdef LZT
freopen("in","r",stdin);
#endif
work();
#ifdef LZT
Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
}
标签:build 就是 需要 amp https ctime getchar 数据 clr
原文地址:https://www.cnblogs.com/wawawa8/p/11105315.html