-
Notifications
You must be signed in to change notification settings - Fork 0
/
BF.c
59 lines (50 loc) · 1.04 KB
/
BF.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 255
/**
* Brute-Force
*
* best T=O(m+n)
* worst T=O(m*n)
*/
typedef struct String{
char c[MAX];
int length;
}String;
int BFMatch(String *s , String *t , int pos){
int i = pos - 1 , j = 1;
int num = 0;
while(i < s->length){
if(s->c[i] == t->c[j - 1]){
if(j == t->length){
num += 1;
j = 1;
}
++i;
++j;
}else{
i = i - j + 2;
j = 1;
}
}
return num;
}
void initString(String *s , String *t){
printf("Primary String:");
scanf("%s" , s->c);
s->length = strlen(s->c);
printf("\nSubstring:");
scanf("%s" , t->c);
t->length = strlen(t->c);
}
int main(){
String *s = (String *)malloc(sizeof(String));
String *t = (String *)malloc(sizeof(String));
initString(s , t);
int pos;
printf("\nLocation:");
scanf("%d" , &pos);
printf("\ntimes:%d" , BFMatch(s , t , pos));
return 0;
}