线性时间算法用于在字符串中查找大多数频繁的m字母substring
假设我们有一个字母字符串,我们正在寻找大多数重复的m字母substring(1 =< m =< n)。 我只是在寻找一种在线性时间解决此问题的算法。我已经到达后缀树。但是如何通过后缀树解决它?
多谢。
Suppose we have a n letter string and we are searching for most repeated m letter substring (1=<m =< n).
I am just searching for an algorithm which solves this problem in linear time. And I have reached to suffix tree. But how can I solve it by suffix tree?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想法
您还可以使用哈希功能解决它。
我们可以将字符串转换为基本
b
数字,其中b
是质量数字。例如,如果字符串仅由小写字母组成(26个字符
az
),那么我们可以选择b
等于29
。然后,我们将字符串字符映射到相应的数字。例如:
字符串
ABC
将等于29^2*1 + 29^1*2 + 29^0*3 = 899
在基本29
中。我们应该映射
a - &gt; 1
,但不是a - &gt; 0
由于aaa
和aa
的哈希值在基本b
中是相等的,这不应该是。现在,我们可以比较基本
b
中的哈希值,而不是比较两个字符串。如果它们的哈希值相等,那么我们可以说它们是相等的。由于哈希值可能很大,因此您可以使用它的模块一个大质量数字,例如
mod 1e9+7
。在这种情况下,两个不同字符串的实体性具有相同的哈希值。算法
可以将算法描述为Bellow:
要计算
hash(s)
,首先,我们构建数组h
其中:shere
s [i]
is映射的字符串字符i-th的字符数量。要计算
b^x
,我们可以计算数组powb
其中:然后,对于子字符串
s [l。 字符串s的.r]
正如我们所看到的,
hash(s)
可能为负,在这种情况下,我们应该将mod
hash添加(s)
(hash(s) += mod
)。复杂
Idea
You can also solve it with hash function.
We can convert strings to base
b
numbers whereb
is a prime number.For example, if the strings only consist of lowercase alphabet (26 characters
a-z
) then we can chooseb
equals29
.Then we map string characters to corresponding numbers. For example:
String
abc
will equals29^2*1 + 29^1*2 + 29^0*3 = 899
in base29
.We should map
a -> 1
but nota -> 0
since hash value ofaaa
andaa
will be equal in baseb
, which shouldn't be.Now instead of compare two strings, we can compares their hash value in base
b
. If their hash value are equal then we can say they are equal.Since hash value can be very large, you can use it's module a large prime number, for example
mod 1e9+7
. The posibility of two different strings have same hash value is very low in this case.Algorithm
The algorithm can be described as bellow:
To calculate
hash(s)
, first we build arrayH
where:Here
S[i]
is the mapped number of character i-th of string S.To calculate
b^x
, we can calculate arraypowb
where:Then for a substring
s[l..r]
of string S,As we can see,
hash(s)
can be negative, in this case we should addmod
tohash(s)
(hash(s) += mod
).Complexity