-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.h
More file actions
58 lines (53 loc) · 1.5 KB
/
Copy pathfs.h
File metadata and controls
58 lines (53 loc) · 1.5 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
57
58
//
// Created by pc on 5/30/2024.
//
#ifndef ESTIMATINGSTRINGPATTERNALGORITHMS_FS_H
#define ESTIMATINGSTRINGPATTERNALGORITHMS_FS_H
#include "include/define.h"
#include "include/main_header.h"
#include "Algorithm.h"
class fs : public Algorithm {
public:
const char* name() const override {
return "FS";
}
void Pre_GS(unsigned char *x, int m, int bm_gs[]) {
int i, j, p, f[XSIZE];
for(i=0;i<XSIZE;i++) bm_gs[i]=0;
f[m]=j=m+1;
for (i=m; i > 0; i--) {
while (j <= m && x[i-1] != x[j-1]) {
if (bm_gs[j] == 0) bm_gs[j]=j-i;
j=f[j];
}
f[i-1]=--j;
}
p=f[0];
for (j=0; j <= m; ++j) {
if (bm_gs[j] == 0) bm_gs[j]=p;
if (j == p) p=f[p];
}
}
int search(unsigned char *x, int m, unsigned char *y, int n) override {
int a,i, j, k, s, count, bc[SIGMA], gs[XSIZE];
char ch = x[m-1];
/* Preprocessing */
for (a=0; a < SIGMA; a++) bc[a]=m;
for (j=0; j < m; j++) bc[x[j]]=m-j-1;
Pre_GS(x, m, gs);
for(i=0; i<m; i++) y[n+i]=ch;
/* Searching */
count = 0;
if( !memcmp(x,y,m) ) count++;
s=m;
while(s<n) {
while((k=bc[y[s]])) s += k;
j=2;
while (j<=m && x[m-j]==y[s-j+1]) j++;
if( j>m && s<n) count++;
s += gs[m-j+1];
}
return count;
}
};
#endif //ESTIMATINGSTRINGPATTERNALGORITHMS_FS_H