[algorithms] 暴力算法(字符串匹配)
一、代码
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4
5
6 int match1(char *source, char *pattern)
7 {
8 int debug = 0;
9 int sl = strlen(source);
10 int pl = strlen(pattern);
11 printf("\n\n");
12 printf("\t[match1_source_length]#\t%d\n", sl);
13 printf("\t[match1_pattern_length]#\t%d\n", pl);
14 // mov: use for source moving
15 int mov = 0;
16 // state: count matched char. if (state == pl) return index in source;
17 int state = 0;
18 // index: using for returning index if source match pattern
19 int index = 0;
20 for (int i=0; i<=sl-pl; i++) {
21 // index: using for returning index if source match pattern
22 mov = i;
23 state = 0;
24 index = i;
25 for (int j=0; j<pl; j++, mov++) {
26 if (source[mov] != pattern[j]) {
27 break;
28 } else {
29 if (debug == 1) {
30 printf("\t[match1_test]#\t%c\n", source[mov]);
31 }
32 state += 1 ;
33 }
34 if(state==pl) {
35 printf("\t[match1]#\tindex[0, %d]\t:=\t%d\n", sl-pl, index);
36 return index;
37 }
38 }
39 }
40 printf("\t[match1]#\tnot match,\t%d\n", -1);
41 return -1;
42 }
43
44
45 int match2(char *source, char *pattern)
46 {
47 int debug = 0;
48 int sl = strlen(source);
49 int pl = strlen(pattern);
50 printf("\n\n");
51 printf("\t[match2_source_length]#\t%d\n", sl);
52 printf("\t[match2_pattern_length]#\t%d\n", pl);
53 // index: for return index;
54 int index = 0;
55 // i,j: control char moving in source or pattern.
56 int i=0, j=0;
57 // state: count times for match pattern in source.
58 int state = 0;
59 while(i < sl && j < pl)
60 {
61 /*
62 if (source[i]==pattern[j]) {
63 i++;
64 j++;
65 state++;
66 if (debug == 1) {
67 printf("\t[match2_test]#\t%c\n", source[i-1]);
68 }
69 } else {
70 index++;
71 i = index;
72 j = 0;
73 state = 0;
74 if (debug == 1) {
75 printf("\t[match2_index]#\t%d\n", index);
76 }
77 }
78 if (state==pl) {
79 printf("\t[match2]#\tindex[0, %d]\t:=\t%d\n", sl-pl, index);
80 return index;
81 }
82 */
83 if (source[i] != pattern[j]) {
84 if (debug == 1) {
85 printf("\t[not_match2_index]#\t%d\n", index);
86 printf("\t[not_match2_state]#\t%d\n", state);
87 }
88 index++;
89 i = index;
90 j = 0;
91 state = 0;
92 } else {
93 if (debug == 1) {
94 printf("\t[match2_test]#\t%c\n", source[i]);
95 printf("\t[match2_index]#\t%d\n", index);
96 }
97 i++;
98 j++;
99 state++;
100 if (debug == 1) {
101 printf("\t[match2_state]#\t%d\n", state);
102 }
103 if (state == pl) {
104 printf("\t[match2]#\tindex[0, %d]\t:=\t%d\n", sl-pl, index);
105 return index;
106 }
107 }
108 }
109
110 printf("\t[match2]#\tnot match,\t%d\n", -1);
111 return -1;
112 }
113
114
115
116 void msg()
117 {
118 char s[] = "abchjhkhlhmhnhi";
119 char p[] = "hnhi";
120 match1(s,p);
121 match2(s,p);
122 }
123
124
125 int main(int argc, char *argv[], char *envp[])
126 {
127 msg();
128
129 return 0;
130 }
二、运行结果
[match1_source_length]# 15
[match1_pattern_length]# 4
[match1]# index[0, 11] := 11
[match2_source_length]# 15
[match2_pattern_length]# 4
[match2]# index[0, 11] := 11
三、参考资料
1、 cpp在线编译器 - https://coliru.stacked-crooked.com/
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/18555907