字符串搜索算法实现

时间:2016-05-25 08:30:43

标签: javascript python string algorithm search

我使用naive方法实现了字符串搜索算法,以计算子字符串在字符串中出现的次数。我在javascript和python中完成了实现。

算法(来自Topcoder):

function brute_force(text[], pattern[]) 
{
  // let n be the size of the text and m the size of the
  // pattern
  count = 0
  for(i = 0; i < n; i++) {
    for(j = 0; j < m && i + j < n; j++) 
      if(text[i + j] != pattern[j]) break;
      // mismatch found, break the inner loop
    if(j == m) // match found
        count+=1
  return count
  }
}

Javascript实施:

a = "Rainbow";
b = "Rain";
count = 0;
function findSubStr(Str, SubStr){
    for (i = 0; i<a.length; i++){
        //document.write(i, '<br/>');
        for (j = 0; j < b.length; j++)
            //document.write('i = ',i, '<br/>');
            //document.write(j, '<br/>');
            if(a[i + j] != b[j]) break;
            document.write('j = ', j, '<br/>')
            //document.write('i = ',i, '<br/>');
    if (j  == b.length)
        count+=1;
    }
    return count;
}
document.write("Count is ",findSubStr(a,b), '<br/>');

Python实现:

a = "Rainbow"
b = "Rain"
def SubStrInStr(Str, SubStr):
    count = 0
    for i in range(len(Str)):
        for j in range(len(SubStr)):
            print j
            if (a[i + j] != b[j]):
                break
        if (j+1 == len(SubStr)):
            count+=1
    return count
print(SubStrInStr(a, b))

现在我的问题是实现if(j == b.length)的行:它在javascript中工作得很好但是对于python我需要在j的值中加1或从长度中扣除1 b。我不知道为什么会这样。

4 个答案:

答案 0 :(得分:3)

for x in range(4)

与Python中的Javascript不同,for循环用于列表中的每个元素。最后一个值x将是列表[0,1,2,3]的最后一个元素,即3。

for(x = 0; x < 4; x++)

在Javascript中,x取4的值,循环结束,因为x&lt; 4条件不再适用。最后一个值x将为4。

答案 1 :(得分:1)

你有这种困惑,因为你的代码不一样。执行public object Property { get; set; } for (j = 0; j < b.length; j++)的最终值将为j(如果b是a的子字符串),但对于Python,情况稍有不同。运行b.length会产生range(len("1234")),因此您的[0, 1, 2, 3]更像是forforeach存储数组中的最后一个值,这就是为什么你必须添加一个。我希望我足够清楚。如果没有,请询​​问详情。

答案 2 :(得分:0)

我对 javascript 一无所知,但我已经用最简单的方法在所有情况下实现了 Python 中的朴素搜索

如下图所示。 它将返回没有找到的时间模式。

def naive_pattern_search(data,search):
n = len(data) #Finding length of data
m = len(search) #Finding length of pattern to be searched.

i = 0
count = c = 0 #Taking for counting pattern if exixts.
for j in range(m-1):#Loop continue till length of pattern to be Search.
    while i <= (n-1):#Data loop
        
        #if searched patten length reached highest index at that time again initilize with 0.
        if j > (m-1):
            j = 0
        
        #Data and search have same element then both Index increment by 1.
        if data[i]==search[j]:
            #print(f"\n{ data[i] } { search[j] }")
            #print(f"i : {i}  {data[i]}   j : {j}  {search[j]}")
            i+=1
            j+=1
            count+=1
            
            #If one pattern compared and found Successfully then Its Counter for pattern.
            if count== (m-1):
                c = c + 1
        #Initilise pattern again with 0 for searching with next element in data.
        else:
            j = 0 #Direct move to 0th index.
            i+=1
            count=0 #If data not found as per pattern continuously then it will start counting from 0 again.

#Searched pattern occurs more then 0 then its Simply means that pattern found.
if c > 0:
    return c;
else:
    return -1;

输入:abcabcabcabcabc 输出:发现模式:5次

答案 3 :(得分:-1)

我发现你的python实现有一些问题。如果设置b =“raiy”,则函数将错误地返回1.您可能会误解边缘条件。 这两个条件陈述应该在同一级别。

a = "Rainbow"
b = "Rain"
def SubStrInStr(Str, SubStr):
   count = 0
   for i in range(len(Str)):
       for j in range(len(SubStr)):
           # print (j)
           if (a[i + j] != b[j]):
               break
           if (j+1 == len(SubStr)):
               count+=1
return count
print(SubStrInStr(a, b))here
相关问题