楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.23-6.29 CareerCup 1.8

🔗
readman 2014-6-24 16:21:14 | 只看该作者
全局:
本帖最后由 readman 于 2014-6-24 16:29 编辑

【解题思路】
boyer moore search
【时间复杂度】
N+M
【空间复杂度】
N
【gist link】https://gist.github.com/gaoyike/e1ce14a8dccc0a300159
【test case】

点评

//www.1point3acres.com/bbs/misc.php?mod=faq: 5.0
积分不够请看帮助 http://www.1point3acres.com/bbs/misc.php?mod=faq: 5
看你的实现,还学习了一下string search algorithms!  发表于 2014-6-25 04:37
回复

使用道具 举报

🔗
heycinderella 2014-6-25 00:28:12 | 只看该作者
全局:
jason51122 发表于 2014-6-24 13:02
Suppose the length of string is N. I think append() operation for StringBuffer will cost 2N, which ...

你说的是时间还是空间?
回复

使用道具 举报

🔗
tonygxxx1212 2014-6-25 04:45:10 | 只看该作者
全局:
【解题思路】
看书后按照书上的来,检查s2是不是s1s1的substring
【时间复杂度】
觉得这个得看你自己的假设了,如果没有实现isSubstring,可以把这个当做O(1)操作,同样concatenate操作也可以当做O(1)[c++ 里, string 的 operator += 操作]
如果实现了isSubstring, 那就得按具体的算法来计算。
【空间复杂度】
O(N)
【gist link】https://gist.github.com/xun-gong/7895504c829d08bba647
回复

使用道具 举报

🔗
ivycheung1208 2014-6-25 08:57:34 | 只看该作者
全局:
【解题思路】
自己没想出来=。=看了书上解答之后茅塞顿开……
if s1 and s2 have same length and s2 is substring of s1s1, then s2 must be a rotation of s1!
为了方便test还自己写了个isSubstring呼……
【时间复杂度】
O(N^2) (subString)
【空间复杂度】
O(N) (concatenation)
【gist link】
https://gist.github.com/f58495f1e17061d43c53
【test case】
empty string -> return false
回复

使用道具 举报

🔗
grassgigi 2014-6-25 09:10:00 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-6-25 09:13 编辑

I think it is O(R), in which R is size of the character set.But you are correct that the total space complexity will be O(n)
回复

使用道具 举报

🔗
jason51122 2014-6-25 11:52:00 | 只看该作者
全局:
heycinderella 发表于 2014-6-25 00:28
你说的是时间还是空间?

Time Complexity
回复

使用道具 举报

🔗
heycinderella 2014-6-26 01:21:18 | 只看该作者
全局:
jason51122 发表于 2014-6-24 13:02
Suppose the length of string is N. I think append() operation for StringBuffer will cost 2N, which ...

啊是我代码的问题,要是这样写是否是O(1)呢?我实在不太会算。。。谢谢!
return isSubstring(str1, (new StringBuilder(str2)).append(str2).toString());
回复

使用道具 举报

🔗
心焰 2014-6-26 04:34:27 | 只看该作者
全局:
【解题思路】
My 1st idea is to use recursion. However this disobeys 'using only one call to isSubstring'.
【时间复杂度】Top-level O(N)
【空间复杂度】O(1)
【gist link】
....
回复

使用道具 举报

🔗
jason51122 2014-6-26 12:58:07 | 只看该作者
全局:
heycinderella 发表于 2014-6-26 01:21
啊是我代码的问题,要是这样写是否是O(1)呢?我实在不太会算。。。谢谢!
return isSubstring(str1, (new ...

Suppose the length of str2 is N. new StringBuffer(str2) will cost N. append(str2) will also cost N. Please refer http://stackoverflow.com/questio ... mple-piece-of-code. Thanks.
回复

使用道具 举报

🔗
guchang 2014-6-26 13:06:51 | 只看该作者
全局:
【解题思路】 rotation可能是S1=S2,或者S1被拦腰截断,后段在前面,前段在后面,所以只要S1+S1,就能囊括rotation的各种可能。rotation结果S2是S1+S1的substring。
【时间复杂度】
O(n)
【空间复杂度】
O(n) 多了一段s1.

地址 https://gist.github.com/guchang/cdf6826ecab11093548e
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表