📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3242| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[字符串] Google Challenge重复删除字符串

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 qetu133 于 2016-5-17 09:14 编辑

我遇到个题目,想了很久都不知道怎么做好点,这个题目就是直接匹配是绝对会超时的,不知道用什么样的方法才能解出来。Your spy, Beta Rabbit, has managed to infiltrate a lab of mad scientists who are turning rabbits into zombies. He sends a text transmission to you, but it is intercepted by a pirate, who jumbles the message by repeatedly inserting the same word into the text some number of times. At each step, he might have inserted the word anywhere, including at the beginning or end, or even into a copy of the word he inserted in a previous step. By offering the pirate a dubloon, you get him to tell you what that word was. A few bottles of rum later, he also tells you that the original text was the shortest possible string formed by repeated removals of that word, and that the text was actually the lexicographically earliest string from all the possible shortest candidates. Using this information, can you work out what message your spy originally sent?
If the final chunk of text was "lolol," and the inserted word was "lol," the shortest possible strings are "ol" (remove "lol" from the beginning) and "lo" (remove "lol" from the end). The original text therefore must have been "lo," the lexicographically earliest string.

Write a function called answer(chunk, word) that returns the shortest, lexicographically earliest string that can be formed by removing occurrences of word from chunk. Keep in mind that the occurrences may be nested, and that removing one occurrence might result in another. For example, removing "ab" from "aabb" results in another "ab" that was not originally present. Also keep in mind that your spy's original message might have been an empty string.

chunk and word will only consist of lowercase letters [a-z].
chunk will have no more than 20 characters.
word will have at least one character, and no more than the number of characters in chunk.

Test cases
==========

Inputs:
   (string) chunk = "lololololo"
   (string) word = "lol"
Output:
   (string) "looo"

Inputs:
   (string) chunk = "goodgooogoogfogoood"
   (string) word = "goo"
Output:
   (string) “dogfood"

我用python写了个 answer 函数,这个函数可以应对这样的问题,但是效率太低了,我实在不知道怎么样才能提高它的效率了。至于为什么超时就是我注解的那两行,一行是O(n)的复杂度,n代表加密后的字符串长度,另一行一行是递归调用,换句话说复杂度=O(插入次数*n)。
def answer(chunk, word):
   if len(chunk) < len(word):
       return chunk
   indexs = []
   #找到符合word的index保存(可以改成KMP算法,依然超时)
   for i in range(len(chunk)-len(word)+1):
       if chunk[i:i+len(word)] == word:
           indexs.append(i)
   ans = chunk
   for i in indexs:
        #逐个index查找最短的原始信息
       tmp = answer(chunk[:i]+chunk[i+len(word):],word)
       if len(tmp) < len(ans) or len(tmp) == len(ans) and tmp < ans:
           ans = tmp
   return ans

print(answer('6bb6b67b7b67b67bbc' ,'b67b'))
print(answer("lololololo","lol"))
print(answer('goodgooogoogfogoood','goo'))
print(answer('0d960750d960d9607500750d960750002a','0d960750'))
=================自动验证脚本==================================
  1. import uuid
  2. import random

  3. def answer(chunk, word):
  4.     # your code here
  5.     if len(chunk) < len(word):
  6.         return chunk
  7.     indexs = []
  8.     for i in range(len(chunk)-len(word)+1):
  9.         if chunk[i:i+len(word)] == word:
  10.             indexs.append(i)
  11.     ans = chunk
  12.     for i in indexs:
  13.         tmp = answer(chunk[:i]+chunk[i+len(word):],word)
  14.         if len(tmp) < len(ans) or len(tmp) == len(ans) and tmp < ans:
  15.             ans = tmp
  16.     return ans

  17. def my_random_string(string_length=10):
  18.     """Returns a random string of length string_length."""
  19.     rand = str(uuid.uuid4()) # Convert UUID format to a Python string.
  20.     rand = rand.replace("-","") # Remove the UUID '-'.
  21.     return rand[0:string_length] # Return the random string.

  22. while 1:
  23.     orig_str = my_random_string(random.randrange(3,5))
  24.     noise_str = my_random_string(random.randrange(2,21))
  25.     if orig_str in noise_str or noise_str in orig_str:
  26.         continue
  27.     insert_num = random.randrange(2,5)
  28.     output_str = orig_str
  29.     for i in xrange(insert_num):
  30.         rand_pos = random.randrange(0,len(output_str))
  31.         output_str = output_str[:rand_pos]+noise_str+output_str[rand_pos:]
  32.     if orig_str != answer(output_str, noise_str):
  33.         print(output_str+" "+orig_str+","+noise_str)
  34.         raw_input()
复制代码


上一篇:leetcode刷过的题目老是忘记解法
下一篇:求亚麻的面筋总结
🔗
stellari 2016-5-17 06:21:44 | 只看该作者
全局:
这题最重要的描述部分你没有贴全吧?
回复

使用道具 举报

🔗
 楼主| qetu133 2016-5-17 08:13:35 | 只看该作者
全局:
stellari 发表于 2016-5-17 06:21
这题最重要的描述部分你没有贴全吧?

这就是最重要的部分啦,就是递归删除子串呢。前面讲的是什么兔博士要解密什么的,然后原文信息被插入很多的杂信息,现在已知加入的pattern,然后寻找原来的最短的信息。
回复

使用道具 举报

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

本版积分规则

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