-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrstr.c
50 lines (44 loc) · 1 KB
/
strstr.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
#include <stdio.h>
#include <stdbool.h>
bool strstr(char *p, char *s) {
for (int i = 0; i < strlen(p); i++) {
for (int j = 0; j < strlen(s); j++) {
if (s[j] != p[i + j]) {
break;
}
return true;
}
}
return false;
}
// KMP
void getNext(char *s, int *next) {
int j = 0;
next[0] = 0;
for (int i = 1; i < strlen(s); i++) {
while (j >0 && s[i] != s[j]) {
j = next[j - 1];
}
if (s[i] == s[j]) {
j++;
}
next[i] = j;
}
}
int strstr(char *str, char *pattern) {
int *next = (int *)malloc(sizeof(int) * strlen(pattern));
getNext(pattern, next);
int j = 0;
for (int i = 0; i < strlen(str); i++) {
while (j > 0 && str[i] != pattern[j]) {
j = next[j - 1];
}
if (str[i] == pattern[j]) {
j++;
}
if (j == strlen(pattern)) {
return i - strlen(pattern) + 1;
}
}
return -1;
}