Brute-Force
字符串匹配算法中最基础的一个又称bf算法
核心思想是
建立一个模式串,在建立一个主串,用模式串去与主串做对比
For example
主串 “abdabc”
模式串“abc”
![](https://img.haomeiwen.com/i4212920/bfc44e5275d7f84c.jpg)
代码表示
#!/user/bin/env python
#-*- coding:utf-8 -*-
#Author : Zhuangzhi Gao
#Date :17/10/2021
src = 'abdabc'
sub = 'abc'
def Brute(str1,str2):
i,j = 0,0
index = 0
while (i<len(str1) and j<len(str2)):
print(i,j)
if str1[i]==str2[j]:
i+=1
j+=1
else:
index+=1
i = index
j = 0
if j == len(str2):
print('BtuteForce is Succaessful')
return index
else:
return -1
print(Brute(src,sub))
网友评论