-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path串匹配BF算法.CPP
More file actions
39 lines (37 loc) · 887 Bytes
/
串匹配BF算法.CPP
File metadata and controls
39 lines (37 loc) · 887 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
#include<bits/stdc++.h>
using namespace std;
int BF(char S[ ], char T[ ]);
int main()
{
char S[]="abcabcabcaccb";
char T[]="abcacc";
int index=BF(S,T);
for (int i = 1; i < index; i++)
cout<<" ";
cout<<T<<"在"<<endl;
cout<<S<<"中的位置是:"<<index<<endl;
return 0;
}
int BF(char S[ ], char T[ ])
{
int index = 0; //主串从下标0开始第一趟匹配
int i = 0, j = 0; //设置比较的起始下标
while ((S[i] != '\0') && (T[j] != '\0'))
{
if (S[i] == T[j])
{
i++;
j++;
}
else
{
index++; //i和j分别回溯
i = index;
j = 0;
}
}
if (T[j] == '\0')
return index + 1; //返回本趟匹配的开始位置(不是下标)
else
return 0;
}