搜索匹配模式和QUOT字符串; ABC:*:XYZ"在不到为O(n) [英] Search for strings matching the pattern "abc:*:xyz" in less than O(n)

查看:131
本文介绍了搜索匹配模式和QUOT字符串; ABC:*:XYZ"在不到为O(n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一串字符串,我需要找到那些匹配了3种模式:

Given a bunch of strings I need to find those which match 3 kinds of patterns:

  • preFIX搜索 - ABC *
  • 水珠状花纹 - 美国广播公司:*:XYZ
  • 后缀搜索 - * XYZ

其中*是通配符(可以匹配任何数量字符的)。

where * is a wildcard (and can match any number of chars).

现在的直接的解决办法就是扫描每个字符串,看看它的目标模式相匹配。但是,这是O(n)。如果我存储在一个平衡搜索树的字符串,我可以在O(log n)的做preFIX查询。如果我创建了一个多树,所有的字符串本质逆转,我可以做在澳后缀查询(log n)的。有没有要搜索的一个聪明的办法ABC:*:XYZ?模式有效地

Now the straight-forward solution is just to scan every string and see if it matches the target pattern. But this is O(n). If I stored the strings in a balanced search tree, I can do the prefix queries in O(log n). If I created one more tree with all the strings essentially reversed, I can do the the suffix queries in O(log n). Is there a clever way to search for the "abc:*:xyz" patterns efficiently?

推荐答案

会不会从其他两个查询结果的交集会给你到底是什么?由于每个结果为O(日志N)和交叉过的结果集为O(N)的结果集的大小,也不会总也是一个O(日志N)在原来的问题?

Wouldn't an intersection of the results from the other two queries give you exactly that? And since each of the results is O(log N) and an intersect over that result set is O(N) in the result-set size, wouldn't the total also be an O(log N) over the original problem?

这篇关于搜索匹配模式和QUOT字符串; ABC:*:XYZ"在不到为O(n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆