English 中文(简体)
无法找到关于Trie 操作的算法( 递归) 。
原标题:Can t figure out this algorithm (recursive) on Trie operation

有人能帮助我理解下面的黄道码吗?

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    

The idea is that using a Trie we are trying to find the number of times a word appears in our dictionary, BUT we may have missing letters.
I am lost in the else part. I don t understand the logic.
For example if the first char of our word is a match we hit the last else and then recurse on countWords in the same level but with missingLetters-1 but then isn t that an identical loop? I.e. it will compare again the first letter in the same level and so on?
Could someone please help me figure this out?

最佳回答

即使最后几行的顺序按照 Antti.huima 的建议被颠倒了, 有些事情在我看来还是不太对劲。

如果我理解正确, 如果你有 < code> Pizza , < code> Lizza 也应该算漏掉Lizza < /code > 的Letters =1 =1, 对不对? 但如果Lizza不在, 您输入

else if notExists(edges[ l ])  
        cutLeftmostCharacter(word) #  izza  left  
        return countWords(vertex,  izza , 0) #vertex is  P  I guess

下一个您输入

else if notExists(edges[ i ]) and missingLetters=0

返回 0?

鉴于你已经有一个三胞胎,我建议你看看Levehnstein距离

问题回答

算法是错误的 。 我怀疑由于某种原因, 最后的切切“ MostCharacter ” 的电话已经和前一行进行了交换。 如果代码会读的话

   cutLeftmostCharacter(word)  
   r=countWords(vertex, word, missingLetters-1)  
   r=r+countWords(edges[k], word, missingLetters)  

这会更有意义。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签