0
21kviews
Explain naive string matching algorithm with example.
1 Answer
written 8.4 years ago by |
NAIVE-STRING-MATCHER(T, P)
1 n length [T]
2 m length [P]
3 for s 0 to n - m
4 do if P [1 . . m] = T[s + 1 . . s + m]
5 then print "Pattern occurs with shift" s
The naive string-matching procedure can be interpreted graphically as sliding a "template" containing the pattern over the text, noting for which shifts all of the characters on the template equal the corresponding characters in the text, as illustrated in Figure.
<centre>
In the following sections, we examine several ways to make effective use of this sort of information. Example1:
1) Input:
txt [] = “THIS IS A TEST TEXT”
pat [] = “TEST”
2) Output:
Pattern found at index 10.
Example2:
1) Input:
txt [] = “AABAACAADAABAAABAA”
pat [] = “AABA”
2) Output:
Pattern found at index 0
Pattern found at index 9
Pattern found at index 13