-
Notifications
You must be signed in to change notification settings - Fork 28
/
minmove.cpp
67 lines (57 loc) · 1.17 KB
/
minmove.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
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define inf 1000000000
#define MAXN 200000
using namespace std;
int p = 10007;
int H[MAXN];
int P[MAXN];
string s;
int limit;
int h(int idx, int length) {
return H[idx+length-1]-H[idx-1]*P[length];
}
int lcp(int mini, int idx) {
if (s[mini] != s[idx]) return 0;
int lo = 1, hi = limit;
while (lo < hi) {
int mid = lo + (hi-lo)/2;
if (h(mini, mid) == h(idx, mid)) lo = mid+1;
else hi = mid;
}
return lo-1;
}
int main() {
cin.sync_with_stdio(false);
cin >> s;
limit = s.size();
s += s;
P[0] = 1;
H[0] = s[0];
int i;
for (i = 1; i < (int)s.size(); ++i) {
P[i] = P[i-1]*p;
H[i] = H[i-1]*p + s[i];
}
int mini = 0;
for (int i = 1; i < limit; ++i) {
int elcepe = lcp(mini, i);
if (s[mini+elcepe] > s[i+elcepe]) mini = i;
}
cout << mini << endl;
return 0;
}