-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathSuffix Automata.cpp
126 lines (101 loc) · 2.44 KB
/
Suffix Automata.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
// Counts number of distinct substrings
struct suffix_automaton
{
map<char, int> to[MAX];
int len[MAX], link[MAX];
int last, psz = 0;
void add_letter(char c)
{
int p = last, cl, q;
if(to[p].count(c))
{
q = to[p][c];
if(len[q] == len[p] + 1)
{
last = q;
return;
}
cl = psz++;
len[cl] = len[p] + 1;
to[cl] = to[q];
link[cl] = link[q];
link[q] = cl;
last = cl;
for(; to[p][c] == q; p = link[p])
to[p][c] = cl;
return;
}
last = psz++;
len[last] = len[p] + 1;
for(; to[p][c] == 0; p = link[p])
to[p][c] = last;
if(to[p][c] == last)
{
link[last] = p;
return;
}
q = to[p][c];
if(len[q] == len[p] + 1)
{
link[last] = q;
return;
}
cl = psz++;
len[cl] = len[p] + 1;
to[cl] = to[q];
link[cl] = link[q];
link[q] = cl;
link[last] = cl;
for(; to[p][c] == q; p = link[p])
to[p][c] = cl;
}
void clear()
{
for(int i = 0; i < psz; i++)
len[i] = 0, link[i] = 0, to[i].clear();
psz = 1;
last = 0;
}
void init(string s)
{
clear();
for(int i = 0; i < s.size(); i++)
add_letter(s[i]);
}
suffix_automaton() {psz = 0; clear();}
};
string s;
suffix_automaton SA;
ll cnt[MAX];
vi endpos[MAX];
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt","r",stdin);
int test, cases=1;
cin>>s;
SA.clear();
FOR(i,0,s.size())
SA.add_letter(s[i]), cnt[SA.last]++;
FOR(i,0,SA.psz)
{
endpos[SA.len[i]].pb(i);
}
ll ans=0;
FORr(i,SA.psz-1,1)
{
for(auto it: endpos[i])
{
cnt[SA.link[it]]+=cnt[it];
ans+=(SA.len[it]-SA.len[SA.link[it]]); // distinct occurrences
// cnt[it] has occurrence of substring ending at node it
}
}
// cnt[x] has occurrences of state x
// To calculate occurrence of an input string, we visit the automata using the letters
// of the input string and find the last_state where it finishes
// The cnt[last_state] should be the occurrence of this string
prnt(ans);
return 0;
}