Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. template <typename T>
  2. class Trie {
  3. private:
  4. T count;
  5. Trie<T> *next[26];
  6. public:
  7. Trie() {
  8. count = T();
  9. for (int i = 0; i < 26; i++) next[i] = NULL;
  10. }
  11. ~Trie() {
  12. for (int i = 0; i < 26; i++) delete next[i];
  13. }
  14. void add(string w) {
  15. if (w.size() == 0) return;
  16. int pos = w[0] - 'a';
  17. if (next[pos] == NULL)
  18. next[pos] = new Trie<int>();
  19. next[pos]->count++;
  20. next[pos]->add(w.substr(1));
  21. }
  22. int find(string w) {
  23. int res = 0, pos;
  24. if (w.size() && next[w[0] - 'a'] != NULL)
  25. return next[w[0] - 'a']->find(w.substr(1));
  26. if (w.size()) return 0;
  27. return this->count;
  28. }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement