-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkmp.cpp
More file actions
89 lines (69 loc) · 1.71 KB
/
kmp.cpp
File metadata and controls
89 lines (69 loc) · 1.71 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
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
void over_lay(const char* str, int* lay_array)
{
int length = 0;
while(*(str + length)) length++;
//num is index
lay_array[0] = -1;
int index = -1;
// lay_array[1] = -1;
//i is the end index
for (int i = 1; i < length; i++) {
if (index == -1) {
lay_array[i] = (str[0] == str[i]) ? 0 : -1;
index = lay_array[i];
continue;
}
//if index != -1;do the following
while (index != -1) {
if ( str[index + 1] == str[i] ){
lay_array[i] = index + 1;
index = index + 1;
break;
}
else if(index >= 0){
index = lay_array[index];
}
}
if(index == -1) lay_array[i] = -1;
// cout << index;
}
}
int kmp(const char* str1, const char* str2)
int length1 = 0;
int length2 = 0;
while( *(str1 + length1)) length1 ++;
while( *(str2 + length2)) length2 ++;
int* lay_array = new int[length2];
over_lay(str2, lay_array);
while( index1 < length1) {
if ( *(str1 + index1) == *(str2 + index2)) {
index1 ++;
index2 ++;
}
else if (index2 == length2) return (index1 - length2);
else if (index2 == 0) index1 ++;
else {
index2 = lay_array[index2] + 1;
}
}
return -1;
}
int main(int argc, char *argv[])
{
char string1[] = "abcdabcd";
char string2[] = "ayyuuiiabcdabcddsadisad";
int length = 0;
while ( *(string1 + length)) length++;
cout << "length:" << length<< endl;
int* lay_array = new int[length];
over_lay(string1, lay_array);
for (int i = 0; i < length; i++) {
cout << lay_array[i] << endl;
}
cout << "answer is : ";
cout << kmp(string2, string1) << endl;
delete[] lay_array;
return 0;
}