-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP_DFA.cpp
More file actions
42 lines (40 loc) · 1.2 KB
/
Copy pathKMP_DFA.cpp
File metadata and controls
42 lines (40 loc) · 1.2 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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
// using namespace std;
class KMP {
private:
std::string pattern;
std::vector<std::vector<int> > dfa;
public: // Access specifier
KMP(std::string pattern)
{
this->pattern = pattern;
int M = pattern.length();
int R = 256;
dfa = std::vector<std::vector<int> >(R, std::vector<int>(M));
dfa[pattern[0]][0] = 1;
for (int X = 0, j = 1; j < M; j++) {
for (int c = 0; c < R; c++)
dfa[c][j] = dfa[c][X]; // Copy mismatch cases.
dfa[pattern[j]][j] = j+1; // Set match case.
X = dfa[pattern[j]][X]; // Update restart state.
}
}
int search(std::string text)
{
int i, j, N = text.length(), M = pattern.length();
for (i = 0, j = 0; i < N && j < M; i++)
j = dfa[text[i]][j];
if (j == M) return 1; // found
return -1; // not found
}
};
int main(int argc, char *argv[])
{
std::cout<< "hello world"<<std::endl;
KMP kmp = KMP("ABABAC");
std::cout<<kmp.search("ABABAC")<<std::endl;
return EXIT_SUCCESS;
}