-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSuffix Automaton
More file actions
40 lines (36 loc) · 1.27 KB
/
Suffix Automaton
File metadata and controls
40 lines (36 loc) · 1.27 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
using System.Collections.Generic;
public class SuffixAutomaton
{
class St { public int Len, Link; public Dictionary<char,int> Next = new(); }
private readonly List<St> st = new() { new St{ Len=0, Link=-1 } };
private int last = 0;
public void Extend(char c)
{
int cur = st.Count; st.Add(new St{ Len = st[last].Len + 1 });
int p = last;
while (p!=-1 && !st[p].Next.ContainsKey(c)) { st[p].Next[c]=cur; p = st[p].Link; }
if (p==-1) st[cur].Link = 0;
else {
int q = st[p].Next[c];
if (st[p].Len + 1 == st[q].Len) st[cur].Link = q;
else {
int clone = st.Count; st.Add(new St{
Len = st[p].Len + 1, Link = st[q].Link, Next = new Dictionary<char,int>(st[q].Next)
});
while (p!=-1 && st[p].Next.GetValueOrDefault(c) == q) { st[p].Next[c]=clone; p = st[p].Link; }
st[q].Link = st[cur].Link = clone;
}
}
last = cur;
}
public SuffixAutomaton(string s){ foreach (var ch in s) Extend(ch); }
public bool Contains(string t)
{
int v=0;
foreach (var ch in t)
{
if (!st[v].Next.TryGetValue(ch, out v)) return false;
}
return true;
}
}