Question: Horspool s algorithm / / Fills the shift table used by Horspool s and Boyer - Moore algorithms / / Input: Pattern P [ 0

Horspools algorithm
//Fills the shift table used by Horspools and Boyer-Moore algorithms
//Input: Pattern P[0..m 1] and an alphabet of possible characters
// For your assignment the set of characters are the ASCII characters 0-127
//Output: Table[0..size 1] indexed by the alphabets characters and
// filled with shift sizes computed by formula (7.1)
ShiftTable(needle [0..m 1])
for i 0 to size 1 do Table[i]m
for j 0 to m 2 do Table[needle[j ]]m 1 j
printShiftTable
return Table
//Implements Horspools algorithm for string matching
//Input: Pattern needle[0..m 1] and text haystack[0..n 1]
//Output: The index of the left end of the first matching substring
// or 1 if there are no matches
HorspoolMatching(needle[0..m 1], haystack[0..n 1])
ShiftTable(needle[0..m 1])//generate Table of shifts
printf("%s
", haystack);
i m 1//position of the patterns right end
while i = n 1 do
k0//number of matched characters
while k = m 1 and needle[m 1 k]= haystack[i k] do
kk +1
if k = m
printf("%*s%s---found
", i-m+1,"", needle);
return i m +1// Except for your code, do not return. Start looking for the next occurance from here.
else
printf("%*s%s
", i-m+1,"", needle);
i i + Table[haystack[i]]
printf("Matches found at locations:");
for(i =0; i matchNum; i++)
{
printf("%d", matches[i]);
}
printf("
");
return 1
#define MAX_ALPHABET 128
#define TABLE_ROW_LENGTH 16
#define MIN_WRITEABLE 32
//Print out the shift table starting at the first writable character (space)
void printShiftTable(int table[])
{
int i, start;
for(start = MIN_WRITEABLE; start MAX_ALPHABET; start+=TABLE_ROW_LENGTH)
{
for(i = start; i start+TABLE_ROW_LENGTH; i++)
{
printf("%c\t", i);
}
printf("
");
for(i = start; i start+TABLE_ROW_LENGTH; i++)
{
printf("%d\t", table[i]);
}
printf("
");
}
Ouput:
abaabcbabcbabcbabbbabcbaabbcbabcabc
abc
abc
abc---found
abc
abc---found
abc
abc---found
abc
abc
abc
abc
abc---found
abc
abc
abc
abc
abc---found
abc---found
Matches found at locations: 3711192932
Horspool s algorithm / / Fills the shift table

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!