我想了解以下算法删除,但我有上几件事情很难:找到了一个词,一个字母是从字
首先是什么输入像即APLE或Ap_le和
其次,这部分代码是做什么的?
“我们正在增加的两种可能性:第一 字符已被删除,加上第一个字符是目前 r=countWords(vertex, word, missingLetters-1)
”
第三不应该第二否则,如果经过连接到顶点的所有edegs?
来源: https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries/
countWords(vertex, word, missingLetters)
k=firstCharacter(word)
if isEmpty(word)
return vertex.word
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
只是一点提示:第二个ifelse只会最多再次调用countWords()。这是因为我们无法再匹配前缀(没有边界),但是我们仍然可以检查(通过调用countWords())如果再删除一个字母就会匹配。意思是,如果我们的单词匹配到目前为止,但最后还有一个额外的字符,那么我们认为它是匹配的(因为我们可以删除它)。 – synepis
该代码需要输入如“aapple”或“appple”,并通过删除单个字母来查看是否可以将其变为单词。 –