Python-什么词可以删除最多的连续字母并且仍然是字典有效词?

标签 python algorithm performance word

我使用这个可怕且低效的实现来找到可以删除最连续的最后一个字母并且仍然是一个单词的单词。

比如Rodeo,就是大家熟知的:Rodeo,Rode,Rod,Ro。 该程序找到了“ Composer ”: Composer 、 Composer 、 Composer 、 Composer 、 Composer

我想知道我应该如何着手创建一个程序来找到最长的单词,该单词可以删除任何字母(不仅仅是最后一个字母)并且仍然被认为是一个单词:

例如:beast、best、bet、be -- 都是有效的可能性

这是我的程序,用于查找删除连续字母的程序(我也有兴趣了解如何改进和优化它):

#Recursive function that finds how many letters can be removed from a word and
#it still be valid.  
def wordCheck(word, wordList, counter):

    if len(word)>=1:
        if word in wordList:
            return (wordCheck(word[0:counter-1], wordList, counter-1))
        else:
            return counter
    return counter


def main():
    a = open('C:\\Python32\\megalist2.txt', 'r+')
    wordList = set([line.strip() for line in a])
    #megaList contains a sorted list of tuple of 
    #(the word, how many letters can be removed  consecutively)
    megaList = sorted([(i, len(i)-1- wordCheck(i, wordList, len(i))) for i in wordList], key= lambda megaList: megaList[1])


    for i in megaList:
        if i[1] > 3:
            print (i)

if __name__ == '__main__':
    main()

最佳答案

for each english word W:
    for each letter you can remove:
        remove the letter
        if the result W' is also word:
            draw a line W->W'
for each english word W:
    connect ROOT-> each english word W
use a graph search algorithm to find the longest path starting at ROOT
    (specifically, the words are now in a directed acyclic graph; traverse
    the graph left-right-top-down, i.e. in a "topological sort", keeping
    track of the longest candidate path to reach each node; this achieves 
    the result in linear time)

这个算法只需要线性的 O(#wordsInEnglish*averageWordLength) 时间!基本上只要读取输入就可以了

它可以很容易地修改以找到删除的连续字母:而不是像 (Node('rod').candidate = rodeo->rode->rod 这样每个节点保留一个候选者),为每个节点保留一系列候选者以及您为获得每个候选者而删除的字母的索引 (Node('rod').candidates={rodeo->rod|e->rod|, road->ro|d}).这具有相同的运行时间。

关于Python-什么词可以删除最多的连续字母并且仍然是字典有效词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084795/

相关文章:

performance - 列出所有 n 对整数的最快解决方案?

python - 为什么 Pylons 使用 StackedObjectProxies 而不是 threading.local?

python - 下面的代码在cpython中做了什么?

algorithm - WinRar 使用了哪种数据压缩算法?

algorithm - 优先处理重叠的日期范围

asp.net - 如何使我的 asp.net 网站 "more cookie free"?

python - 如何计算每月变化并将其绘制为 varchar

python - 尝试从 AWS Lambda 连接到 Boto3 客户端并接收超时

algorithm - 我的算法仅对大值失败 - 我该如何调试?

performance - 我可以在一个监听器中查看 2 个请求的结果吗?