0
所以我做了一個擁有相當大量數據的trie,我的搜索算法相當快,但我想看看是否有人對我如何更快地實現它有所瞭解。C++ Trie搜索性能
bool search (string word)
{
int wordLength = word.length();
node *current = head;
for (unsigned int i=0; i<wordLength; ++i)
{
if (current->child[((int)word[i]+(int)'a')] == NULL)
return false;
else
current = current->child[((int)word[i]+(int)'a')];
}
return current->is_end;
}
我使用+(int)a,因爲有值低於 – 2012-03-01 03:34:13
的字符@that_guy:在這種情況下,您不應該向「word [i]」添加任何內容。決定有效範圍,並(可選)將範圍從「word [i]」減去範圍中的最小值,從0開始。 – tom 2012-03-01 04:22:30