Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <math.h>
- #include <string>
- #include <iostream>
- #include <set>
- #include <queue>
- using namespace std;
- struct Trie
- {
- Trie *children[30];
- bool is;
- Trie()
- {
- is = false;
- for (int i = 0; i < 30; i++)
- children[i] = NULL;
- }
- };
- void Insert(Trie *root, string key)
- {
- Trie *temp = root;
- for (auto &c : key)
- {
- if (temp->children[c - 'a'] == NULL)
- temp->children[c - 'a'] = new Trie();
- temp = temp->children[c - 'a'];
- }
- temp->is = true;
- }
- bool Search(Trie *root, string key)
- {
- Trie *temp = root;
- for (auto &c : key)
- {
- if (temp->children[c - 'a'] == NULL)
- return false;
- temp = temp->children[c - 'a'];
- }
- if(temp->is == true) return true;
- return false;
- }
- int main()
- {
- Trie *root = new Trie();
- Insert(root, "abc");
- Insert(root, "adc");
- Insert(root, "bdc");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement