Bzoj 1717(字符串Hash/后缀数组)

BZOJ 1717
字符串Hash做法:
每次二分一个答案,然后Hash每一个答案长度的子串,比较看有没有$k$个相同的Hash值即可.
Hash做法TLE了两个点.

后缀数组做法:
每次二分一个答案,然后在$height$数组里分组,看有没有连续$k$个相同长度的$height$即可.

字符串Hash:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
#define ull unsigned long long
using namespace std;
const int MAXN = 1000000 + 5;
const ull MO1 = 19260817, MO2 = 19660813, bs = 10007;
struct data {
ull a, b;
bool operator < (const data &y) const {
if (a == y.a) return b < y.b;
return a < y.a;
}
}h[MAXN];
int n, k, a[MAXN];
ull hash1(int st, int len) {
ull ret = 0;
for (int i = st; i < st + len; i++) {
ret = (ret * bs + a[i]) % MO1;
}
return ret;
}
ull hash2(int st, int len) {
ull ret = 0;
for (int i = st; i < st + len; i++) {
ret = (ret * bs + a[i]) % MO2;
}
return ret;
}
bool check(int x) {
for (int i = 1; i <= n - x + 1; i++) h[i].a = hash1(i, x), h[i].b = hash2(i, x);
sort(h + 1, h + 1 + n - x + 1);
int tot = 0;
for (int i = 2; i <= n - x + 1; i++) {
if (h[i].a == h[i - 1].a && h[i].b == h[i - 1].b) tot++;
else {
tot++;
if (tot >= k) return true;
tot = 0;
}
}
tot++;
if (tot >= k) return true;
return false;
}
void clean() {
}
void solve() {
clean();
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int l = 0, r = n + 1, ans = 0;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid)) ans = mid, l = mid + 1; else r = mid;
}
printf("%d\n", ans);
}
int main() {
scanf("%d%d", &n, &k), solve();
return 0;
}

后缀数组(很久没打。。已经不太会了):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
#define ull unsigned long long
using namespace std;
const int MAXN = 1000000 + 5;
int n, m, k, a[MAXN];
int rk[MAXN], tp[MAXN], tax[MAXN], SA[MAXN], height[MAXN];
bool cmp(int *f, int i, int k) {return f[SA[i - 1]] == f[SA[i]] && f[SA[i - 1] + k] == f[SA[i] + k];}
void getSA() {
for (int i = 0; i < m; i++) tax[i] = 0;
for (int i = 0; i < n; i++) tax[rk[i] = a[i]]++;
for (int i = 1; i < m; i++) tax[i] += tax[i - 1];
for (int i = n - 1; i >= 0; i--) SA[--tax[rk[i]]] = i;
int p;
for (int k = 1; k <= n; k *= 2) {
p = 0;
for (int i = n - k; i < n; i++) tp[p++] = i;
for (int i = 0; i < n; i++) if (SA[i] >= k) tp[p++] = SA[i] - k;
for (int i = 0; i < m; i++) tax[i] = 0;
for (int i = 0; i < n; i++) tax[rk[tp[i]]]++;
for (int i = 1; i < m; i++) tax[i] += tax[i - 1];
for (int i = n - 1; i >= 0; i--) SA[--tax[rk[tp[i]]]] = tp[i];
swap(rk, tp), rk[SA[0]] = 0, p = 0;
for (int i = 1; i < n; i++) {
rk[SA[i]] = cmp(tp, i, k) ? p : ++p;
}
if (++p >= n) break;
m = p;
}
}
void getHeight() {
int j, k = 0;
for (int i = 0; i < n; i++) {
if (k) k--;
j = SA[rk[i] - 1];
while (a[i + k] == a[j + k]) k++;
height[rk[i]] = k;
}
}
bool check(int x) {
int tot = 0;
for (int i = 1; i < n; i++) {
if (height[i] >= x) {
tot++;
if (tot + 1 >= k) return true;
} else tot = 0;
}
return false;
}
void clean() {}
void solve() {
clean();
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
m = 1000010, a[n] = 0, n++;
getSA(), getHeight();
int ans = 0, l = 0, r = n + 1;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid)) ans = mid, l = mid + 1; else r = mid;
}
printf("%d\n", ans);
}
int main() {
scanf("%d%d", &n, &k), solve();
return 0;
}

------ 本文结束 ------