-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHorspool.java
65 lines (53 loc) · 1.84 KB
/
Horspool.java
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
/*
* FILENAME : Horspool.java
* Problem Statement:
* Horspool string matching algorithm
* ------------------------------------------------------------------------------
* AUTHOR : GANESH PAI, Dept. of CS&E, NMAMIT, Nitte
* YEAR : 2021
* E-mail : ganesh.pai@nitte.edu.in
* ------------------------------------------------------------------------------
*/
import java.util.*;
public class Horspool {
public static int table[] = new int[127];
private void shifttable(String pattern)
{
int m = pattern.length();
char p[] = pattern.toCharArray();
Arrays.fill(table, m);
for (int i = 0; i < m - 1; i++)
table[p[i]] = m - 1 - i;
}
public int horspool(String source, String pattern)
{
int m;
char s[] = source.toCharArray();
char p[] = pattern.toCharArray();
m = pattern.length();
shifttable(pattern);
for (int i = m - 1; i < source.length();)
{
int k = 0;
while ( (k < m) && (p[m - 1 - k] == s[i - k]) )
k++;
if (k == m)
return i - m + 2; //Return position
else
i += table[s[i]];
}
return -1;
}
public static void main(String[] args) {
System.out.print("Enter the source string: ");
String source = new Scanner(System.in).nextLine();
System.out.print("Enter a pattern string: ");
String pattern = new Scanner(System.in).nextLine();
Horspool h = new Horspool();
int pos = h.horspool(source, pattern);
if (pos == -1)
System.out.println("Pattern NOT found");
else
System.out.println("Pattern found at Position: " + pos + "\n");
}
}