- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathKnuthMorrisPrattSearcher.cs
81 lines (73 loc) · 2.06 KB
/
KnuthMorrisPrattSearcher.cs
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
usingSystem.Collections.Generic;
namespaceAlgorithms.Strings.PatternMatching;
publicclassKnuthMorrisPrattSearcher
{
/// <summary>
/// An implementation of Knuth–Morris–Pratt Algorithm.
/// Worst case time complexity: O(n + k)
/// where n - text length, k - pattern length.
/// </summary>
/// <param name="str">The string to look in.</param>
/// <param name="pat">The pattern to look for.</param>
/// <returns>
/// The zero-based positions of all occurrences of <paramref name="pat" /> in <paramref name="str" />.
/// </returns>
publicIEnumerable<int>FindIndexes(stringstr,stringpat)
{
varlps=FindLongestPrefixSuffixValues(pat);
for(inti=0,j=0;i<str.Length;)
{
if(pat[j]==str[i])
{
j++;
i++;
}
if(j==pat.Length)
{
yieldreturni-j;
j=lps[j-1];
continue;
}
if(i<str.Length&&pat[j]!=str[i])
{
if(j!=0)
{
j=lps[j-1];
}
else
{
i+=1;
}
}
}
}
/// <summary>
/// Return the longest prefix suffix values for pattern.
/// </summary>
/// <param name="pat">pattern to seek.</param>
/// <returns>The longest prefix suffix values for <paramref name="pat" />.</returns>
publicint[]FindLongestPrefixSuffixValues(stringpat)
{
varlps=newint[pat.Length];
for(inti=1,len=0;i<pat.Length;)
{
if(pat[i]==pat[len])
{
len++;
lps[i]=len;
i++;
continue;
}
if(len!=0)
{
len=lps[len-1];
}
else
{
lps[i]=0;
i++;
}
}
returnlps;
}
}