-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStammeringAliens.cpp
executable file
·97 lines (76 loc) · 2.07 KB
/
StammeringAliens.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
#include <bits/stdc++.h>
template<typename T> T gcd(T a, T b) {
if(!b) return a;
return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(args...)
#else
#define debug(args...) fprintf(stderr,args)
#endif
typedef long long Int;
typedef unsigned long long uInt;
typedef unsigned uint;
const int MAXN = 40010;
const uInt BASE = 7883ULL;
const uInt C_BASE = 4549ULL;
int N;
string S;
uInt hsh[MAXN];
uInt pwd[MAXN];
uInt cmp[MAXN];
void build(const string& arg) {
hsh[0] = 0ULL;
pwd[0] = 1ULL;
cmp[0] = 1ULL;
for (int i = 1; i <= (int) arg.size(); i++) {
hsh[i] = hsh[i - 1] * BASE + (arg[i - 1] - 'a');
pwd[i] = pwd[i - 1] * BASE;
// cmp[i] = cmp[i - 1] * BASE;
}
}
uInt getHash(int l, int r) {
return hsh[r] - hsh[l] * pwd[r - l] + cmp[r - l];
}
int main(void) {
while (cin >> N && N != 0) {
cin >> S;
build(S);
int ans = 0, pos = 0;
int l = 1, h = (int) S.size(), m;
while (l <= h) {
m = (l + h) / 2;
unordered_map<uInt, int> cnt;
int r_most = 0;
bool ok = false;
for (int i = 0; i + m <= (int) S.size(); i++) {
uInt c_hash = getHash(i, i + m);
cnt[c_hash] += 1;
if (cnt[c_hash] >= N) {
ok = true;
r_most = i;
}
}
if (ok) {
ans = m;
pos = r_most;
l = m + 1;
} else {
h = m - 1;
}
}
if (ans > 0) {
cout << ans << " " << pos << "\n";
} else {
cout << "none\n";
}
}
return 0;
}