-
Notifications
You must be signed in to change notification settings - Fork 2
/
SuffixAutomaton.cpp
226 lines (197 loc) · 5.37 KB
/
SuffixAutomaton.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma comment (linker, "/STACK:128000000")
//#include "testlib.h"
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
#include <ctime>
#include <stack>
#include <queue>
using namespace std;
//#define FILENAME ""
#define mp make_pair
#define all(a) a.begin(), a.end()
typedef long long li;
typedef long double ld;
void solve();
void precalc();
clock_t start;
//int timer = 1;
int main() {
#ifdef room111
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#else
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//freopen(FILENAME".in", "r", stdin);
//freopen(FILENAME ".out", "w", stdout);
#endif
int t = 1;
cout.sync_with_stdio(0);
precalc();
cout.precision(10);
cout << fixed;
//cin >> t;
start = clock();
int testNum = 1;
while (t--) {
//cout << "Case #" << testNum++ << ": ";
solve();
//++timer;
}
#ifdef room111
cerr << "\n\n" << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\n\n";
#endif
return 0;
}
//BE CAREFUL: IS INT REALLY INT?
//#define int li
void precalc() {
}
int gcd (int q, int w) {
while(w) {
q %= w;
swap(q, w);
}
return q;
}
int binpow (int q, int w, int mod) {
if (!w)
return 1 % mod;
if (w & 1) {
return (q * binpow(q, w - 1, mod)) % mod;
}
return binpow((q * q) % mod, w / 2, mod);
}
const int UNDEFINED_VALUE = -1;
class SuffixAutomaton {
public:
struct State {
map<char, int> transitions;
int link;
int maxLen;
int firstPos, lastPos;
int cnt;
State():link(UNDEFINED_VALUE), firstPos(UNDEFINED_VALUE), lastPos(UNDEFINED_VALUE), maxLen(0), cnt(0) {}
};
vector<State> states;
int lastState;
SuffixAutomaton(const string& s) {
states.push_back(State());
lastState = 0;
for (int i = 0; i < s.length(); ++i)
append(s[i]);
vector<pair<int, int>> p(states.size());
for (int i = 0; i < p.size(); ++i) {
p[i].second = i;
p[i].first = states[i].maxLen;
}
sort(all(p));
reverse(all(p));
for (int i = 0; i < p.size(); ++i) {
int curState = p[i].second;
if (states[curState].lastPos == UNDEFINED_VALUE)
states[curState].lastPos = states[curState].firstPos;
if (states[curState].link != UNDEFINED_VALUE) {
states[states[curState].link].lastPos = max(states[states[curState].link].lastPos, states[curState].lastPos);
states[states[curState].link].cnt += states[curState].cnt;
}
}
}
int countDoubleOccurrences() {
li res = 0;
for (int curState = 0; curState < states.size(); ++curState) {
int maxLen = states[curState].maxLen;
int minLen = 1;
if (states[curState].link != UNDEFINED_VALUE)
minLen = states[states[curState].link].maxLen + 1;
maxLen = min(states[curState].lastPos - states[curState].firstPos, maxLen);
res += max(maxLen - minLen + 1, 0);
}
return res;
}
vector<int> getSimilarityClasses() {
vector<int> res;
for (int curState = 0; curState < states.size(); ++curState) {
int maxLen = states[curState].maxLen;
int minLen = -1;
if (states[curState].link != UNDEFINED_VALUE)
minLen = states[states[curState].link].maxLen;
res.push_back(maxLen - minLen);
}
sort(all(res));
return res;
}
int findInterestingSubstring(int k) {
int res = 0;
for (int curState = 0; curState < states.size(); ++curState) {
int maxLen = states[curState].maxLen;
if (states[curState].cnt >= k)
res = max(res, maxLen);
}
return res;
}
void dfs(int v, string curString = "") {
if (curString.size() != states[v].maxLen)
return;
cout << v << ' ' << states[v].maxLen << ' ' << curString << ' ' << states[v].firstPos << ' '<< states[v].lastPos << endl;
for (auto to : states[v].transitions)
dfs(to.second, curString + to.first);
}
private:
void append(char c) {
int curState = states.size();
states.push_back(State());
states[curState].maxLen = states[lastState].maxLen + 1;
states[curState].firstPos = states[lastState].maxLen;
states[curState].cnt = 1;
int prevState = lastState;
for (; prevState != UNDEFINED_VALUE; prevState = states[prevState].link) {
if (states[prevState].transitions.count(c))
break;
states[prevState].transitions[c] = curState;
}
if (prevState == UNDEFINED_VALUE) {
states[curState].link = 0;
}
else {
int nextState = states[prevState].transitions[c];
if (states[nextState].maxLen == states[prevState].maxLen + 1) {
states[curState].link = nextState;
}
else {
int cloneState = states.size();
states.push_back(State());
states[cloneState].maxLen = states[prevState].maxLen + 1;
states[cloneState].link = states[nextState].link;
states[cloneState].firstPos = states[nextState].firstPos;
states[curState].link = states[nextState].link = cloneState;
states[cloneState].transitions = states[nextState].transitions;
for (; prevState != UNDEFINED_VALUE && states[prevState].transitions[c] == nextState; prevState = states[prevState].link)
states[prevState].transitions[c] = cloneState;
}
}
lastState = curState;
}
};
void solve() {
int n, k;
string s;
cin >> n >> k;
cin >> s;
SuffixAutomaton automaton(s);
//automaton.dfs(0);
/*vector<int> similarityClasses = automaton.getSimilarityClasses();
for (int item : similarityClasses)
cout << item << ' ';*/
cout << automaton.findInterestingSubstring(k);
}