You are viewing a single comment's thread. Return to all comments →
One more c++ on Prefix Tree.
class OrderSet{ struct Tries{ unordered_map children; bool endOfWorld;
Tries(): endOfWorld(false){}; };
public: Tries * root; OrderSet(): root (new Tries()){};
void insert(string str){ Tries * current = root; for(auto c: str){ if(current->children.find(c) == current->children.end()){ current->children[c]= new Tries(); } current = current->children[c]; } current->endOfWorld=true; }
bool insertCheck(string str){ Tries * current = root; for(int i=0; i < str.size(); i++){ if(current->children.find(str[i]) == current->children.end()){ current->children[str[i]]= new Tries(); } else{ if(i+1 == str.size()) return false; } if(current->endOfWorld){ return false; } current = current->children[str[i]]; } current->endOfWorld=true; return true; }
};
void noPrefix(vector words) {
OrderSet tree; tree.insert(words[0]); int i; bool bad_set=false; for(i=1; i < words.size(); i++){ if(!tree.insertCheck(words[i])){ bad_set = true; break; } }
if(bad_set){ cout << "BAD SET" << endl; cout << words[i] << endl; } else { cout << "GOOD SET" << endl; } }
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
One more c++ on Prefix Tree.
class OrderSet{ struct Tries{ unordered_map children; bool endOfWorld;
Tries(): endOfWorld(false){}; };
public:
Tries * root; OrderSet(): root (new Tries()){};
void insert(string str){ Tries * current = root; for(auto c: str){ if(current->children.find(c) == current->children.end()){ current->children[c]= new Tries(); } current = current->children[c]; } current->endOfWorld=true;
}
bool insertCheck(string str){ Tries * current = root; for(int i=0; i < str.size(); i++){ if(current->children.find(str[i]) == current->children.end()){ current->children[str[i]]= new Tries(); } else{ if(i+1 == str.size()) return false; } if(current->endOfWorld){ return false; } current = current->children[str[i]]; } current->endOfWorld=true; return true;
}
};
void noPrefix(vector words) {
OrderSet tree; tree.insert(words[0]); int i; bool bad_set=false; for(i=1; i < words.size(); i++){ if(!tree.insertCheck(words[i])){ bad_set = true; break; }
}
if(bad_set){ cout << "BAD SET" << endl; cout << words[i] << endl; } else { cout << "GOOD SET" << endl; } }