avatar
求问一道算法题~# JobHunting - 待字闺中
c*x
1
Question:
There are 1000 distinct strings, each string is composed by the chars in{‘a
','b','c','d'}. And each string.Length()==30;
here's a function aims at calculating the overlap of two different strings;
int overlapNumber(string str1, string str2){...}
overlapNumber is the length of largest prefix of str2 which is contained by
str1(Not necessary the surfix of str1)
and what I want is to calculate all the overlapNumbers between every two
strings within this string sets.
Notice: overlap(str1,str2) is sometimes not equal to overlap(str2,str1).
=========================================================================
My Solution:
My solution is somehow straightforward:
I change a little bit of the KMP string match algorithm and calculate every
two strings within this sets.
This takes O(n^2)
=========================================================================
Any Da Shen has a better solution?
I'm thinking of using tries...will it help?
Thanks!
avatar
c*x
2
新人球罩,召唤大神,
avatar
c*x
3
补充:
这题的优化只能在两两string的求overlap上面优化吗?有没有什么方法可以在整个
string set上面优化神马的?
avatar
z*s
4
想了一会没想出来。。。不过你这数据也太没诱惑力了,已经能1000^2*60做出来了,
实在没动力想了。
avatar
x*a
5
优化overlapNumber+整体.
给每一个str做他的suffix tree. 然后就正常搜
avatar
s*e
6

我也觉得应该是这样优化。

【在 x*****a 的大作中提到】
: 优化overlapNumber+整体.
: 给每一个str做他的suffix tree. 然后就正常搜

avatar
g*e
7
为什么你的solution只有n2时间?每对str的kmp运算不止常数时间吧?

‘a
by

【在 c**********x 的大作中提到】
: Question:
: There are 1000 distinct strings, each string is composed by the chars in{‘a
: ','b','c','d'}. And each string.Length()==30;
: here's a function aims at calculating the overlap of two different strings;
: int overlapNumber(string str1, string str2){...}
: overlapNumber is the length of largest prefix of str2 which is contained by
: str1(Not necessary the surfix of str1)
: and what I want is to calculate all the overlapNumbers between every two
: strings within this string sets.
: Notice: overlap(str1,str2) is sometimes not equal to overlap(str2,str1).

avatar
c*x
8

嗯 应该是n^2 * n

【在 g*********e 的大作中提到】
: 为什么你的solution只有n2时间?每对str的kmp运算不止常数时间吧?
:
: ‘a
: by

avatar
c*x
9

错了。。
是 n^2 * length

【在 g*********e 的大作中提到】
: 为什么你的solution只有n2时间?每对str的kmp运算不止常数时间吧?
:
: ‘a
: by

avatar
c*x
10

弱弱的问一句,suffix tree怎么写啊?看了一晚上了。。。

【在 x*****a 的大作中提到】
: 优化overlapNumber+整体.
: 给每一个str做他的suffix tree. 然后就正常搜

avatar
C*w
11
用个简单的dp可以求得str1 str2的overlap,复杂度是O(length)
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。