-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabin_karp.cpp
More file actions
77 lines (61 loc) · 1.46 KB
/
rabin_karp.cpp
File metadata and controls
77 lines (61 loc) · 1.46 KB
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
// BOJ 3033 가장 긴 문자열
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = (1 << 19) - 1;
bool compare(int i, int j, int length, string &s) {
while (length--) {
if (s[i] != s[j])
return false;
i++;
j++;
}
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
string s;
cin >> n >> s;
int base = 31;
int ans = 0;
int left = 1;
int right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
ll hash = 0;
ll k = 1;
for (int i = 0; i < mid; i++) {
hash = (hash * base + s[i]) % MOD;
k = k * base % MOD;
}
vector<vector<int>> v(MOD);
v[hash].push_back(0);
bool flag = false;
for (int i = mid; i < n; i++) {
hash = ((hash * base + s[i] - k * s[i - mid]) % MOD + MOD) % MOD;
for (int j : v[hash]) {
if (compare(j, i - mid + 1, mid, s)) {
flag = true;
break;
}
}
if (flag)
break;
v[hash].push_back(i - mid + 1);
}
if (flag) {
ans = max(ans, mid);
left = mid + 1;
} else
right = mid - 1;
}
cout << ans;
}