-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringPrefix.cpp
93 lines (84 loc) · 1.49 KB
/
StringPrefix.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define eps 1e-9
#define all(a) a.begin(),a.end()
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define sz size()
#define rd(inp) scanf("%lld",&inp)
#define rd2(inp1, inp2) scanf("%lld %lld",&inp1, &inp2)
#define rl(inp) scanf("%d",&inp)
#define pf(out) printf("%lld\n", out);
const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int inf = 1e9;
ll read(){
bool minus = false;
ll result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus){
return -result;
}
else{
return result;
}
}
ll fpow(ll base,ll power){
ll result = 1;
while (power > 0){
if (power%2 == 1) result=(result*base);
base = (base*base);
power /= 2;
}
return result;
}
#define maxn 1000100
ll lps[maxn];
void Prefix(string str){
ll i, j, n = str.sz;
i = 0, j = 1;
lps[0] = 0;
while ( i < n && j < n ){
if ( str[i] == str[j] ){
lps[j] = i + 1;
j ++; i ++;
}
else{
if ( i > 0 ){
i = lps[i-1];
}
else{
lps[j] = 0;
j ++;
}
}
if ( j == n ) break;
}
}
int main(){
string str;
cin >> str;
Prefix(str);
ll i;
cout << str << "\n";
for ( i = 0 ; i < str.sz ; i ++ ){
cout << lps[i] << " ";
}
cout << "\n";
return 0;
}