【发布时间】:2012-05-25 05:04:47
【问题描述】:
有人可以帮我理解以下 pheudocode 吗?
countWords(vertex, word, missingLetters)
k=firstCharacter(word)
if isEmpty(word)
return vertex.words
else if notExists(edges[k]) and missingLetters=0
return 0
else if notExists(edges[k])
cutLeftmostCharacter(word)
return countWords(vertex, word, missingLetters-1)
//Here we cut a character but we don't go lower in the tree
else
//We are adding the two possibilities: the first
//character has been deleted plus the first character is present
r=countWords(vertex, word, missingLetters-1)
cutLeftmostCharacter(word)
r=r+countWords(edges[k], word, missingLetters)
return r
我们的想法是使用Trie,我们试图找出一个单词在我们的字典中出现的次数,但我们可能缺少字母。
我迷失在else 部分。我不明白其中的逻辑。
例如,如果我们单词的第一个字符是匹配的,我们会点击最后一个 else,然后在同一级别上递归 countWords,但使用 missingLetters-1,但这不是一个相同的循环吗? IE。它会再次比较同一级别的第一个字母等等?
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: algorithm data-structures dictionary computer-science trie