forked from xiaoyazi333/data-structure-and-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorspool.cpp
More file actions
56 lines (48 loc) · 1.27 KB
/
Copy pathhorspool.cpp
File metadata and controls
56 lines (48 loc) · 1.27 KB
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
#include <bits/stdc++.h>
using namespace std;
/**********************************************
horspool
**********************************************/
#define ALPHABET (256)
void print_matching_result(const char *p, int s)
{
for(int i = 0; i < s; i++)
printf(" ");
printf("%s\n",p);
}
void calc_skip(int skip[], const char *p)
{
int np = strlen(p);
for(int i = 0; i < ALPHABET; i++)
skip[i] = np;
for(int i = 0; i < np-1; i++)
skip[p[i]] = np-i-1;
}
void horspool(const char *t, const char *p)
{
printf("**********************************************\n");
printf("%s\n",t);
int s = 0, nt = strlen(t), np = strlen(p), skip[ALPHABET];
calc_skip(skip, p);
while(s <= nt-np)
{
int j = np-1;
while(j >= 0 && p[j] == t[s+j])
j--;
if(j < 0)
print_matching_result(p, s);
s = s+skip[t[s+np-1]];
}
}
int main()
{
horspool("ABABABABC", "ABAB");
horspool("ABABCABAB", "ABAB");
horspool("AAAAAAA", "AAA");
horspool("ABABABC", "ABABC");
horspool("XYXZdeOXZZKWXYZ", "WXYZ");
horspool("GCAATGCCTATGTGACCTATGTG", "TATGTG");
horspool("AGATACGATATATAC", "ATATA");
horspool("CATCGCGGAGAGTATAGCAGAGAG", "GCAGAGAG");
return 0;
}