python数据结构与算法1: BF算法

# -*- coding: utf-8 -*-
"""
BF algoritm
To count the string numer appears in another string.
Author @Eric Lv
e-mail: [email protected]
"""

# the main string
t = "this is a big appleappl,this is a big appleapple,this is a big apple,this is a big apple."
# searching string
p = "apple"
'''
t="为什么叫向量空间模型呢?其实我们可以把每个词给看成一个维度,而词的频率看成其值(有向),即向量,这样每篇文章的词及其频率就构成了一个i维空间图,两个文档的相似度就是两个空间图的接近度。假设文章只有两维的话,那么空间图就可以画在一个平面直角坐标系当中,读者可以假想两篇只有两个词的文章画图进行理解。"
p="读者"
'''
### main function
i = 0;
j = 0;
count = 0;

while( i <= len(t)-len(p) ):      
    j = 0;
    while(t[i] == p[j]):
        i = i + 1;
        j = j + 1;

        if( j == len(p) ):
            count += 1;
            j = 0;
            i = i - 1;
            break;
    i = i + 1;


print("The word 'apple' appears in the string 't' in the number of ", count)

你可能感兴趣的:(算法)