1
+ #include <stdio.h>
2
+ #include <string.h>
3
+ #include "search.h"
4
+
5
+ int bruteFS1 (const char * text , const char * pattern ) {
6
+ int n = strlen (text );
7
+ int m = strlen (pattern );
8
+
9
+ for (int i = 0 ; i <= n - m ; i ++ ) {
10
+ int j = 0 ;
11
+ while (j <= m - 1 && pattern [j ] == text [i + j ]) {
12
+ j ++ ;
13
+ }
14
+ if (j == m ) {
15
+ return i ;
16
+ }
17
+ }
18
+ return -1 ;
19
+ }
20
+
21
+ int bruteFS2 (const char * text , const char * pattern ) {
22
+ int n = strlen (text );
23
+ int m = strlen (pattern );
24
+
25
+ const char c = pattern [0 ];
26
+
27
+ int i = 0 ;
28
+ while (i <= n - m && text [i ] != c ) {
29
+ i ++ ;
30
+ }
31
+
32
+ while (i <= n - m ) {
33
+ int j = 0 ;
34
+ while (j <= m - 1 && pattern [j ] == text [i + j ]) {
35
+ j ++ ;
36
+ }
37
+ if (j == m ) {
38
+ return i ;
39
+ }
40
+ i ++ ;
41
+ }
42
+ return -1 ;
43
+ }
44
+
45
+ int searchWithHelperFunc1 (const char * text , const char * pattern ) {
46
+ int n = strlen (text );
47
+ int m = strlen (pattern );
48
+
49
+
50
+ return strncmp (pattern , text , m );
51
+
52
+
53
+
54
+
55
+
56
+ }
57
+ int searchWithHelperFunc2 (const char * text , const char * pattern ){
58
+ int n = strlen (text );
59
+ int m = strlen (pattern );
60
+
61
+ const char c = pattern [0 ];
62
+
63
+ int i = 0 ;
64
+ while (i <= n - m && text [i ] != c ) {
65
+ i ++ ;
66
+ }
67
+
68
+ return -1 ;
69
+
70
+ }
71
+
72
+ int mp (const char * text , const char * pattern ) {
73
+ int n = strlen (text );
74
+ int m = strlen (pattern );
75
+
76
+ // Pre-processing: construct bonPref table
77
+
78
+ // Double loop: use bonPref table to skip comparaisons
79
+
80
+ }
81
+
82
+ int kmp (const char * text , const char * pattern ) {
83
+ int n = strlen (text );
84
+ int m = strlen (pattern );
85
+
86
+ // Pre-processing: construct MeilPref table
87
+
88
+ // Double loop: use bonPref table to skip comparaisons
89
+
90
+ }
91
+ int main () {
92
+
93
+ const char * text = "abcabcbabcabcabcaa" ;
94
+ const char * pattern = "bcaa" ;
95
+
96
+ // printf("bruteForce: %d\n", bruteFS1(text, pattern));
97
+ printf ("bruteForce: %d\n" , searchWithHelperFunc1 (text , pattern ));
98
+
99
+
100
+ return 0 ;
101
+ }
0 commit comments