简体   繁体   English

字符串匹配算法设计

[英]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]. 给定文本t [1 ... n]和k个模式p1,p2,... pk,每个模式的长度为m,n = 2m,来自字母[0,Sigma-1]。 Design an efficient algorithm to find all locations i in t where any of the patterns pj's match. 设计一种有效的算法,以找到t中任何与模式pj匹配的位置i。

So I have a string t = "1 2 3 4 5 2 2 9" and the pattern p = "4 5 2 2". 所以我有一个字符串t =“ 1 2 3 4 5 2 2 9”和模式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...). 我知道会有m + 1个位置可以找到一个模式(从“ 1 2 3 4”,“ 2 3 4 5”等开始)。

Then we have k characters in the pattern so the bigO comes outs to be O(k(m+1)). 然后我们在模式中有k个字符,因此bigO得出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. 这将对m + 1个位置运行k次迭代。

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. 这将对m + 1个位置运行k次迭代。

That means for each pattern, you can do it O(m+1), right? 这意味着对于每个模式,您都可以做到O(m + 1),对吗?

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)). 您有m + 1个位置,并且对于每个位置都需要检查m个字符,因此每个模式的总复杂度为O(m(m + 1))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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