-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultikmp.c
118 lines (115 loc) · 2.33 KB
/
Multikmp.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Statistic{
int mem;
long long cmpnum;
}Statistic;
Statistic global_stats;
void* bupt_malloc(size_t size){
if (size <= 0) {
return NULL;
}
global_stats.mem += size;
return malloc(size);
}
void getnext(char *p, int *next)
{
int len = strlen(p);
int k = -1;
int j = 0;
next[0] = -1;
while (j < len - 1)
{
if (k == -1 || p[k] == p[j])
{
k++;
j++;
next[j] = k;
}
else{
k = next[k];
}
}
}
int kmp(char *s, char *p, int *next)
{
int i = 0;
int j = 0;
int sLen = strlen(s);
int pLen = strlen(p);
int occNum = 0;
while (i < sLen){
if (j >= pLen){
occNum++;
j = 0;
}
if (j == -1 || s[i] == p[j]){
i++;
j++;
global_stats.cmpnum++;
}
else{
j = next[j];
}
}
if (j >= pLen){
occNum++;
}
return occNum;
}
int main(){
global_stats.cmpnum = 0;
global_stats.mem = 0;
FILE *fp;
int strLen = 0;
char* strLine = (char*)bupt_malloc(900*1024*1024);
if ((fp = fopen("./string.txt", "r")) == NULL){
return -1;
}
while (!feof(fp)){
fgets(strLine, 900 * 1024 * 1024, fp);
while (strLine[strLen] != '\n'&&strLine[strLen] != '\0'){
strLen++;
}
strLine[strLen] = '\0';
}
fclose(fp);
if ((fp = fopen("./pattern_bf_kmp.txt", "r")) == NULL){
return -1;
}
FILE* fp1 = fopen("./result.txt", "w");
while (!feof(fp)){
char patternLine[100] = { '\0' };
fgets(patternLine, 100, fp);
int patternLen = 0;
while (patternLine[patternLen] != '\n'&&patternLine[patternLen] != '\0'){
patternLen++;
}
patternLine[patternLen] = '\0';
int *patternNext = (int*)bupt_malloc(sizeof(int)*patternLen);
getnext(patternLine, patternNext);
int occNum = kmp(strLine, patternLine, patternNext);
fprintf(fp1, "%s", patternLine);
fprintf(fp1, "%c", ',');
fprintf(fp1, "%d", occNum);
fprintf(fp1, "%c", '\n');
}
fprintf(fp1, "%s", "字符/字节比较次数:");
fprintf(fp1, "%d", global_stats.cmpnum / 1000);
fprintf(fp1, "%c", '\n');
fprintf(fp1, "%s", "内存开销:");
fprintf(fp1, "%d", global_stats.mem / 1000);
fclose(fp);
fclose(fp1);
/*int next[50], n;
char s[100] = "受力过程同图5非团截面机构受 O二一刀书炭=机构子刀{V机构256";
char p[50] = "机构";
printf("\n实现如下:");
printf("\n s[] =%s:", s);
printf("\n p[] =%s:", p);
getnext(p, next);
n = kmp(s, p, next);
printf("\n匹配的位置为 %d", n);*/
return 0;
}