-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.c
More file actions
56 lines (51 loc) · 953 Bytes
/
kmp.c
File metadata and controls
56 lines (51 loc) · 953 Bytes
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
#include<stdio.h>
char ch1[10000];
char ch2[10000];
int next[10000];
int find(char * ch1,char * ch2){
int i = 0;
while(ch1[i] != '\0' ){
int j = 0;
while(ch2[j] != '\0' ){
if(ch1[i] == ch2[j]){
j++;
i++;
}else{
j = next[j];
i++;
}
}
if(ch2[j] == '\0'){
return i;
}
}
if(ch1[i] == '\0'){
return -1;
}
}
void generate(){
int j = 0;
int count = 0;
int i = 0;
while( ch2[j] != '\0'){
j++;
if(ch2[i] == ch2[j]){
i++;
count++;
next[j] = count;
}else{
i = 0;
count = 0;
if(ch2[j] == ch2[i]){
count++;
}
next[j] = count;
}
}
return;
}
int main(){
scanf("%s",ch1);
scanf("%s",ch2);
return 0;
}