简体   繁体   中英

String matching algorithm design

Given a text t[1...n] and k pattern p1,p2,...pk each of length m, n=2m, from alphabet [0, Sigma-1]. Design an efficient algorithm to find all locations i in t where any of the patterns pj's match.

So I have a string t = "1 2 3 4 5 2 2 9" and the pattern p = "4 5 2 2". I know there will be m+1 locations I can find a pattern (either from "1 2 3 4", "2 3 4 5", etc...).

Then we have k characters in the pattern so the bigO comes outs to be O(k(m+1)).

My algorithm would be to search through the string checking each character with the characters in the pattern. That will run me k iterations for m+1 locations.

Hopefully, I'm explaining it correctly. I just want to know if I'm doing it right and if there are any flaws in my logic. Thank you!

My algorithm would be to search through the string checking each character with the characters in the pattern. That will run me k iterations for m+1 locations.

That means for each pattern, you can do it O(m+1), right?

Although there are algorithms that can achieve this performance, your brute force one isn't. You have m+1 locations, and for each location you need to check m characters, so the total complexity for each pattern is O(m(m+1)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM