Skip to content

Commit b688264

Browse files
committed
Implement knuth morris pratt
1 parent 0845069 commit b688264

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using Algorithms.Strings.Search;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace AlgorithmsUnitTest.Strings.Search
7+
{
8+
public class BoyerMooreUnitTest
9+
{
10+
11+
private ITestOutputHelper console;
12+
13+
public BoyerMooreUnitTest(ITestOutputHelper console)
14+
{
15+
this.console = console;
16+
}
17+
18+
[Fact]
19+
public void Test()
20+
{
21+
String text = "o fare thee well, poor devil of a Sub-Sub, whose commen- \n" + "tator I am. Thou belongest to that hopeless, sallow tribe \n"
22+
+ "which no wine of this world will ever warm ; and for whom \n" + "even Pale Sherry would be too rosy-strong ; but with whom \n"
23+
+ "one sometimes loves to sit, and feel poor-devilish, too ; and \n"
24+
+ "grow convivial upon tears ; and say to them bluntly with full \n" + "eyes and empty glasses, and in not altogether unpleasant \n"
25+
+ "sadness Give it up, Sub-Subs ! For by how much the more \n" + "pains ye take to please the world, by so much the more shall \n"
26+
+ "ye forever go thankless ! Would that I could clear out \n" + "Hampton Court and the Tuileries for ye ! But gulp down \n"
27+
+ "your tears and hie aloft to the royal-mast with your hearts ; \n" + "for your friends who have gone before are clearing out the \n"
28+
+ "seven-storied heavens, and making refugees of long-pampered \n" + "Gabriel, Michael, and Raphael, against your coming. Here \n"
29+
+ "ye strike but splintered hearts together there, ye shall \n" + "strike unsplinterable glasses! ";
30+
31+
BoyerMoore bm = new BoyerMoore("the");
32+
print("found at " + bm.Search(text));
33+
Assert.NotEqual(-1, bm.Search(text));
34+
}
35+
36+
private void print(String content)
37+
{
38+
Console.WriteLine(content);
39+
console.WriteLine(content);
40+
}
41+
}
42+
}

cs-algorithms/Strings/Search/BoyerMoore.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ public BoyerMoore(string pat)
2121

2222
public int Search(string text)
2323
{
24-
int skip = 1;
24+
int skip;
2525
for (int i = 0; i < text.Length - M; i+=skip)
2626
{
27-
var j;
28-
for (j = M - 1; j >= 0; j--)
27+
skip = 0;
28+
for (var j = M - 1; j >= 0; j--)
2929
{
3030
if (text[i + j] != pat[j])
3131
{
3232
skip = Math.Max(1, j - right[text[i + j]]);
3333
break;
3434
}
3535
}
36-
if (j == -1) return i;
36+
if (skip == 0) return i;
3737
}
3838
return -1;
3939
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Algorithms.Strings.Search
2+
{
3+
public class KnuthMorrisPratt
4+
{
5+
private const int R = 256;
6+
private int M;
7+
private int[][] dfs;
8+
public KnuthMorrisPratt(string pat)
9+
{
10+
M = pat.Length;
11+
dfs = new int[R][];
12+
for (var i = 0; i < R; ++i)
13+
{
14+
dfs[i] = new int[M];
15+
}
16+
dfs[0][0] = 1;
17+
int X = 0;
18+
for (var i = 1; i < M; ++i)
19+
{
20+
for (var r = 0; r < R; ++i)
21+
{
22+
dfs[r][i] = dfs[r][X];
23+
}
24+
dfs[pat[i]][i] = i + 1;
25+
X = dfs[pat[i]][X];
26+
}
27+
}
28+
29+
public int Search(string text)
30+
{
31+
int N = text.Length;
32+
int X = 0;
33+
for (var i = 0; i < N; ++i)
34+
{
35+
X = dfs[text[i]][X];
36+
if (X == M) return i;
37+
}
38+
return -1;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)