-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHorspool_Algorithm.cpp
97 lines (78 loc) · 1.94 KB
/
Horspool_Algorithm.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
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <timer.h>
#include <string.h>
class Case
{
char text[10001],pattern[10001];
int textlen,patternlen,shiftTable[95];
void prepareShiftTable();
public :
Case()
{
textlen = patternlen = 0;
}
void input();
int Horspool_Machine();
void randomize();
};
void Case :: input()
{
cout << "\nGive the text : ";
cin >> text;
cout << "\nGive the pattern to be searched : ";
scanf("%s",pattern);
textlen = strlen(text);patternlen = strlen(pattern);
}
void Case :: prepareShiftTable()
{
for(int i = 0;i < 95;i++) shiftTable[i] = patternlen;
for(int j = 0;j < patternlen - 1;j++)
shiftTable[pattern[j] - '0'] = patternlen - j - 1;
}
int Case :: Horspool_Machine()
{
prepareShiftTable();
int i = patternlen - 1;
while(i < textlen)
{
int k = 0;
while(k < patternlen && pattern[patternlen - 1 - k] == text[i - k])
k++;
if(k == patternlen) return (i - patternlen + 1);
else i += shiftTable[text[i] - '0'];
}
return -1;
}
void Case :: randomize()
{
Timer t;
for(int i = 1000;i <= 10000;i += 1000)
{
pattern[1] = '\0';
cout << "\nFor operation : " << i;
textlen = i;patternlen = (int)( textlen / ((rand() % 10) + 1) );
for(int j = 0;j < textlen - 1;j++) text[j] = (rand() % 26) + 'a';
strrev(text);
strncpy(pattern,text,patternlen);
strrev(pattern);
strrev(text);
t.start();Horspool_Machine();t.stop();
cout << " time taken : " << t.time() << " seconds";t.reset();
}
}
int main()
{
clrscr();
Timer t;
int index;
Case C;
C.input();
t.start();index = C.Horspool_Machine();t.stop();
if(index == -1) cout << "\nMatch couldn't be found\n";
else cout << "\nMatch found at position "<< (index + 1) <<"\n";
cout << "\nTime taken for this is : "<< t.time() <<" seconds\n";
getch();
return 0;
}