forked from ireader/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.c
73 lines (62 loc) · 1.07 KB
/
kmp.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
// Knuth-Morris-Pratt Algorithm
// http://www.ics.uci.edu/~eppstein/161/960227.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static void kmp_overlap(const char* pattern, int n, int* overlap)
{
int i, j;
overlap[0] = 0;
for(i=0, j=1; j<n; j++)
{
assert(i < n);
if(pattern[j] == pattern[i])
{
overlap[j] = ++i;
}
else
{
i = 0;
overlap[j] = 0;
}
}
}
static const char* kmp_match(const char* s, const char* pattern, int n1, int n2, int* overlap)
{
int i, j;
i = 0;
j = 0;
while(i < n1 && j<n2)
{
//assert(i+j >= 0 && i+j<n1);
if(s[i] == pattern[j])
{
++j;
++i;
}
else
{
j = j>0?overlap[j-1]:0;
i += j>0?0:1;
}
}
assert(i>=j);
return j==n2?s+i-j:0;
}
const char* kmp(const char* s, const char* pattern)
{
int n1, n2;
int* overlap;
const char* p;
assert(pattern);
n1 = strlen(s);
n2 = strlen(pattern);
overlap = (int*)malloc(sizeof(int)*(n2+1));
if(!overlap)
return NULL;
kmp_overlap(pattern, n2, overlap);
p = kmp_match(s, pattern, n1, n2, overlap);
free(overlap);
return p;
}