0% found this document useful (0 votes)
3 views1 page

String Match

The document contains a Python implementation of the Boyer-Moore string search algorithm, specifically focusing on the bad character heuristic. It defines functions to find the maximum of two numbers, initialize the bad character array, and perform the pattern search in a given text. The code includes user input prompts for the text and pattern to search for, but has some indentation and logical errors.

Uploaded by

jinugv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

String Match

The document contains a Python implementation of the Boyer-Moore string search algorithm, specifically focusing on the bad character heuristic. It defines functions to find the maximum of two numbers, initialize the bad character array, and perform the pattern search in a given text. The code includes user input prompts for the text and pattern to search for, but has some indentation and logical errors.

Uploaded by

jinugv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

MAX_CHARS = 256

def max(a,b):
return a if a>b else b

def badCharHeuristic(pat,size,badchar):
for i in range(MAX_CHARS):
badchar[i] = -1
for i in range(size):
badchar[ord(pat[i])] = i

def patternsearch(text,pat):
m=len(pat)
n=len(text)
badchar=-1*MAX_CHARS

badCharHeuristic(pat,m,badchar)

s=0
while s <= (n-m):
j = m-1

while j >= 0 and pat[j] == text[s+j]:


j -= 1

if j<0:
print("\nPattern occurs at position =",s)
s += m - badchar[ord(text[s + m])] if (s+m)<n else 1
else:
s += max(1,j-badchar[ord(text[s+j])])
text = input("enter the text:").rstrip('\n')
pat=input("enter the pattern:").rstrip('\n')

patternsearch(text,pat)

You might also like