python代码的时间复杂度,用于查找可以由列表中的其他单词组成的最长单词(后续)(Time complexity of python code for finding the longest word that can be made of other words in the list (follow-up))
我知道一个类似的问题已经被提出并解决了( python代码的时间复杂度,用于查找可以由列表中的其他单词组成的最长单词 ),但我有一个后续问题。
我有以下问题,
# Given a list of words, find the longest word made of other words in the list. # Example: [cat, banana, dog, nana, walk, walker, dogwalker] # Result: dogwalker
我在Python 3中有以下实现:
def find_longest_word(list_words): list_words = sorted(list_words, key=lambda x:len(x))[::-1] for elem in list_words: set_remaining = set(list_words) - set([elem]) if find_if_composite(elem, set_remaining, {}): return elem return None def find_if_composite(s, set_): n = len(s) if len(set_) == 0 or n == 0: return False if s in set_: return True for i in range(1, n+1): this_s = s[:i] if this_s in set_: if find_if_composite(s[i:], set_): # assuming that I can reuse the string return True return False
我们从前面的答案中知道这个代码在O(N!)中运行,其中N是字符串的大小(如果我是正确的)。
我的问题是:有没有办法改善时间复杂度(例如,使用memoization)? 如果没有,为什么? 如果有,怎么样? 我尝试了以下代码,但似乎它在递归调用期间从未命中过备忘录。
def find_if_composite_memo(s, set_, memo): n = len(s) if len(set_) == 0 or n == 0: return False if s in memo: return memo[s] if s in set_: return True for i in range(1, n+1): this_s = s[:i] if this_s in set_: if find_if_composite_memo(s[i:], set_, memo): memo[s] = True memo[s[i:]] = True return True memo[s] = False return False
要测试,请使用
b = ["cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "dogwalkerbanana"] print(find_longest_word(b))
I know that a similar question has already been asked and solved (Time complexity of python code for finding the longest word that can be made of other words in the list), but I have a follow-up question.
I have the following problem,
# Given a list of words, find the longest word made of other words in the list. # Example: [cat, banana, dog, nana, walk, walker, dogwalker] # Result: dogwalker
I have the following implementation in Python 3:
def find_longest_word(list_words): list_words = sorted(list_words, key=lambda x:len(x))[::-1] for elem in list_words: set_remaining = set(list_words) - set([elem]) if find_if_composite(elem, set_remaining, {}): return elem return None def find_if_composite(s, set_): n = len(s) if len(set_) == 0 or n == 0: return False if s in set_: return True for i in range(1, n+1): this_s = s[:i] if this_s in set_: if find_if_composite(s[i:], set_): # assuming that I can reuse the string return True return False
We know from the previous answer that this code runs in O(N!) where N is the size of the string (if I am correct).
My question is: is there any way to improve the time complexity (e.g., using memoization)? If not, why? If yes, how? I tried the following code but it seems like it never hits the memo during recursive calls.
def find_if_composite_memo(s, set_, memo): n = len(s) if len(set_) == 0 or n == 0: return False if s in memo: return memo[s] if s in set_: return True for i in range(1, n+1): this_s = s[:i] if this_s in set_: if find_if_composite_memo(s[i:], set_, memo): memo[s] = True memo[s[i:]] = True return True memo[s] = False return False
To test, use
b = ["cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "dogwalkerbanana"] print(find_longest_word(b))
原文:https://stackoverflow.com/questions/35463014
最满意答案
《【穿书】主角的萌宠》by叶楚
相关问答
更多-
哇~你是不昨天提问要类似 协议搅基30天 的呀~
-
校园耽美文,受不苏不找白,《阎王》《狼行成双》《暗恋成双》《岁月间》之类的[2022-11-19]
重生之何以非凡,丰盛时光,小龙女不女,玉石非玉,所谓青梅竹马等 -
请推荐类似于巫哲的《狼行成双》的耽美文 或类似的作者 谢谢[2022-07-31]
周而复始还不错吧,她写的都是现耽,也不太虐的,文笔挺棒的 -
重生寻宝小说[2021-12-30]
淘道 作者: 雨日 简介: 一个在逆境中被病魔缠绕从绝望中奋起,因一次奇遇接触了修真,从此开始了他崭新人生的新篇章!有奇遇、有飞剑、有法宝、有丹药、有冒险、有都市、有言情,更有外星。是一个初涉创作者的梦想,一个逆境中崛起的成功案例! -
像《重生寻宝》这一类的古玩、鉴宝、赌石txt[2023-07-20]
古玩、鉴宝、赌石 建议看打眼和罗晓的书 这两个都是大神级的 -
求受重生成寻宝鼠的耽美文[2023-06-26]
《【穿书】主角的萌宠》by叶楚 -
重生古董类小说:主角按记忆寻宝,女主很多,结局很好,有个女儿,已完本,哪位大侠知道告诉下,谢谢[2023-08-23]
我也再找那个!!! -
那本书主角带着寻宝鼠重生异界[2023-12-02]
绝对经典的少男少女情爱故事会 作者: 书凡 简介: 启开信封,印着朵朵梅花的淡红色信笺轻轻滑落。一行行秀雅的字映入眼帘,我心一动,那字体是久违的熟悉的,出自芸之手。眼前的字迹模糊了,我的思绪飘向了往日……那是一段快乐而又心伤的日子。 -
寻主角有寻宝鼠的小说[2022-12-07]
奇门相师 秦学兵 牛逼的人物 地主婆的发家生活 神医圣手 从岛主到国王 重生之寻宝鼠 超级宠物制造池