We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classTrie{public:boolinsert(stringword){if(!head){head.reset(make_node(L'\0'));}boolfoundPrefix=true;autoworking=head.get();for(autoc:word){constautoidx=c-'a';if(working->isLeaf){// hit a prefix trying to insert the wordreturntrue;}if(!working->children[idx]){// we are appending to the trie, so no prefix was found.working->children[idx].reset(make_node(c));foundPrefix=false;}working=working->children[idx].get();}if(working){working->isLeaf=true;}returnfoundPrefix;}private:structNode{chardata;std::unique_ptr<Node>children[10];boolisLeaf;};Node*make_node(chard){returnnewNode{d,{},false};}std::unique_ptr<Node>head{};};voidnoPrefix(vector<string>words){Triet;for(autoword:words){if(t.insert(word)){printf("BAD SET\n");printf("%s\n",word.c_str());return;}}printf("GOOD SET\n");}
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
It can be solved with a custom trie.