【BZOJ 1717】【USACO06DEC】牛奶模式Milk Patterns(后缀数组)

Description

click me

Solution

后缀数组经典题,直接在height[]上二分即可。

另外,ymy巨佬用后缀自动机也A掉了这题,真是太爷啦!!!

Code

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/****************************
* Au: Hany01
* Prob: bzoj1717
* Date: Jan 31st, 2018
* Email: hany01@foxmail.com
****************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif
inline void File() {
#ifdef hany01
freopen("bzoj1717.in" , "r" , stdin);
freopen("bzoj1717.out" , "w" , stdout);
#endif
}
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
char c_; int _ , __;
inline int read() {
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int MAXN = 20005, SIGMA = 1000005;
int n, m, k, c[SIGMA], sa[MAXN], rk[SIGMA], tp[SIGMA], s[MAXN], height[MAXN];
inline void Radix_Sort()
{
For(i, 1, m) c[i] = 0;
For(i, 1, n) ++ c[rk[i]];
For(i, 2, m) c[i] += c[i - 1];
Fordown(i, n, 1) sa[c[rk[tp[i]]] --] = tp[i];
}
inline void Build_SA()
{
For(i, 1, n) rk[i] = s[i], tp[i] = i;
Radix_Sort();
for (register int k = 1, p; k <= n; k <<= 1) {
p = 0;
For(i, n - k + 1, n) tp[++ p] = i;
For(i, 1, n) if (sa[i] > k) tp[++ p] = sa[i] - k;
Radix_Sort(), swap(tp, rk);
rk[sa[1]] = 1, m = 1;
For(i, 2, n) rk[sa[i]] = tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + k] == tp[sa[i - 1] + k] ? m : ++ m;
if (m == n) break;
}
}
inline void Build_Height() {
for (int i = 1, j, k = 0; i <= n; height[rk[i ++]] = k)
for (k = k ? k - 1 : 0, j = sa[rk[i] - 1]; s[j + k] == s[i + k]; ++ k);
}
inline bool check(int len) {
int cnt = 1;
For(i, 2, n) if (height[i] >= len) {
if (++ cnt == k) return 1;
} else cnt = 1;
return 0;
}
inline void Binary_Search()
{
register int l = 1, r = n, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check(mid)) l = mid; else r = mid - 1;
}
printf("%d\n", l);
}
int main()
{
File();
n = read(), k = read();
For(i, 1, n) s[i] = read(), chkmax(m, s[i]);
Build_SA(), Build_Height();
Binary_Search();
return 0;
}
Contents
  1. 1. Description
  2. 2. Solution
  3. 3. Code