查找具有特定字符串后缀的字符串在最佳时间内?
我们有一个数组“ a”字符串和一个字符串阵列“ b”。对于B中的每个字符串“ S”,我们必须在“ A”中找到具有“ S”的“ A”中的字符串数?
a≤10^51≤b≤10
^51≤1≤10
| ai |≤10^21≤10^
21≤10| bi |≤10^2
的1≤Sizesize naive方法只是通过'b'穿越,而对于每个字符串在b上迭代a以找到一个数字,但时间复杂性为n^2。我们需要一个具有更好时间复杂的解决方案。
We have an array ‘A’ of strings and an array ‘B’ of strings. For each string ‘s’ in B, we have to find the number of strings in ‘A’ which have the suffix as ‘s’?
1≤size of A ≤10^5
1≤size of B ≤10^5
1≤|Ai|≤10^2
1≤|Bi|≤10^2
The naive approach is simply traversing through 'B' and for each string in B iterate over A to find a number but it has a time complexity of N^2. We need a solution with better time complexity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构造A 前缀树基于
a
。在树的每个顶点中,还保留了有关其中数量“通过”的信息。然后,对于
b
中的每个s
,在前缀树中找到一个与s
对应的顶点,然后只读来自>的多少个字符串
通过它(已经存在的信息)。将来自
a
的单词添加到前缀树的反转,以便您可以在后缀和前缀上操作。时间复杂度为
o(size(a) + size(b))
伪代码:
编辑:
由
size(a)
我的意思是a
中的所有字符串中的总数Construct a prefix tree based on
A
. In each vertex of the tree also keep information on how many strings 'pass' through it.Then, for each
s
inB
, find a vertex in the prefix tree that corresponds tos
and just read how many strings fromA
passed through it (the information that is already there).Add words from
A
to prefix tree reversed, so you can operate on suffixes, and not prefixes.Time complexity is
O(size(A) + size(B))
Pseudo code:
EDIT:
By
size(A)
I mean total number of chars in all strings inA
您可以在O(n)时间中执行此操作,其中n是输入的大小(字符串数 *平均长度),而没有任何复杂的数据结构。
首先将所有字符串从后缀问题更改为前缀问题。现在,任务是在A中找到A中的字符串数,而B中的每个字符串作为前缀。
对于B中的每个字符串s,请记住这两个字符串:
例如,如果S为“ ABC”,则S_START =“ ABC”和S_END =“ ABD”。如果我们有“ ABC”< = X< “ ABD”,然后“ ABC”是X的前缀。
现在对A进行排序,对B启动列表进行排序,然后对B端的列表进行排序。如果使用radix排序,则需要O(n)时间。
然后浏览三个列表,以便将它们合并到一个排序列表中。如果在多个列表中找到相同的字符串,请在字符串之前处理B字符串。
跟踪随着消耗的数量,并且:
完成后,对于b,对于b,对于b,对于b的每个s, end [s] - 启动[s]是a中的字符串数,作为前缀。
You can do this in O(N) time, where N is the size of the input (number of strings * average length), and without any complicated data structures.
First reverse all the strings to change this from a suffix problem into a prefix problem. The task is now to find the number of strings in A with each string in B as a prefix.
For each string s in B, remember these two strings:
For example, if s is "abc", then s_start = "abc" and s_end = "abd". Iff we have "abc" <= x < "abd", then "abc" is a prefix of x.
Now sort A, sort the list of B starts, and sort the list of B ends. If you use a radix sort, this takes O(N) time.
Then walk through the 3 lists in order as if you were merging them into one sorted list. If you find the same string in multiple lists, process the B strings before A strings.
Keep track of the number of As you consume, and:
When you're done, for each s in B, end[s]-start[s] is the number of strings in A with s as a prefix.