Я использовал video, чтобы понять префикс trie (хотя в конце концов я пытаюсь добраться до суффикса trie в конце), однако ссылка на образец кода сломана, поэтому я подошел с этим из видео, есть две функции, т.е. вставки и поиска, как показано нижеПостроение префикса Trie в C++, Suffix Trie
void insert(string word)
{
node* current=head;
current->prefix_count++;
for(unsigned int i=0;i<word.length();++i)
{
int letter=(int)word[i]-(int)'a';
if (current->child[letter]==NULL)
current->child[letter]=new node();
current->child[letter]->prefix_count++;
current=current->child[letter];
}
current->is_end=true;
}
bool search(string word)
{
node *current=head;
for(int i=0;i<word.length();++i)
{
if(current->child[((int)word[i]-(int)'a')]==NULL)
return false;
current=current->child[((int)word[i]-(int)'a')];
}
return current->is_end;
}
Затем реализуется основной следующим образом:
int main(){
node* head=NULL;
string s="abbaa";
init();
insert(s);
if(search("ab")==true) cout<<"Found"<<endl;
else cout<<"Not found"<<endl;
}
и я получаю следующий вывод: не найдены
Это сбивает с толку, так как ab находится в строке s.
И, наконец, я пытаюсь понять эту строку:
int letter=(int)word[i]-(int)'a';
означает ли это, мы получаем код ASCII для «а», а затем вычесть из ASCII код текущего символа?
Спасибо