Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- int K = 26;
- struct node
- {
- vector<node*> child;
- bool leaf = false;
- int freq = 0;
- node()
- {
- child.assign(26, nullptr);
- }
- };
- node* root = new node();
- void add(string s)
- {
- node* cur = root;
- for(char c : s)
- {
- if(cur->child[c-'a'] == nullptr) cur->child[c-'a'] = new node();
- cur = cur->child[c-'a'];
- }
- cur->leaf = true;
- cur->freq++;
- }
- bool contains(string s)
- {
- node* cur = root;
- for(char c : s)
- {
- if(cur->child[c-'a'] == nullptr) return false;
- cur = cur->child[c-'a'];
- }
- return true;
- }
- int main()
- {
- add("abc");
- add("abd");
- add("bad");
- add("badc");
- cout << "Contains abd? "<<(contains("abd")?"Yes.\n":"No.\n");
- cout << "Contains badd? "<<(contains("badd")?"Yes.\n":"No.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment