-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash3.h
More file actions
76 lines (67 loc) · 1.86 KB
/
Copy pathhash3.h
File metadata and controls
76 lines (67 loc) · 1.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// Created by pc on 5/30/2024.
//
#ifndef ESTIMATINGSTRINGPATTERNALGORITHMS_HASH3_H
#define ESTIMATINGSTRINGPATTERNALGORITHMS_HASH3_H
#include "include/define.h"
#include "include/main_header.h"
#include "Algorithm.h"
#define RANK3 3
class hash3 : public Algorithm {
public:
const char *name() const override {
return "Hash3";
}
int search(unsigned char *x, int m, unsigned char *y, int n) override {
int count, j, i, sh, sh1, mMinus1, mMinus2, shift[WSIZE];
unsigned char h;
if (m < 3){
KMP kmp;
return kmp.search(x, m, y, n);
}
count = 0;
mMinus1 = m - 1;
mMinus2 = m - 2;
/* Preprocessing */
for (i = 0; i < WSIZE; ++i)
shift[i] = mMinus2;
h = x[0];
h = ((h << 1) + x[1]);
h = ((h << 1) + x[2]);
shift[h] = m - RANK3;
for (i = RANK3; i < mMinus1; ++i) {
h = x[i - 2];
h = ((h << 1) + x[i - 1]);
h = ((h << 1) + x[i]);
shift[h] = mMinus1 - i;
}
h = x[i - 2];
h = ((h << 1) + x[i - 1]);
h = ((h << 1) + x[i]);
sh1 = shift[h];
shift[h] = 0;
if (sh1 == 0) sh1 = 1;
/* Searching */
i = mMinus1;
memcpy(y + n, x, m);
while (1) {
sh = 1;
while (sh != 0) {
h = y[i - 2];
h = ((h << 1) + y[i - 1]);
h = ((h << 1) + y[i]);
sh = shift[h];
i += sh;
}
if (i < n) {
j = 0;
while (j < m && x[j] == y[i - mMinus1 + j]) j++;
if (j >= m) {
OUTPUT(i - mMinus1);
}
i += sh1;
} else return count;
}
}
};
#endif //ESTIMATINGSTRINGPATTERNALGORITHMS_HASH3_H