Advertisement
Manioc

trie

Feb 5th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6.     bool end;
  7.     struct node* word[26];
  8. } *head;
  9.  
  10. void init(){
  11.     head = new node();
  12.     head->end = false;
  13. }
  14.  
  15. void add(string palavra){
  16.     node* current = head;
  17.     for(int i = 0; i < palavra.size(); i++){
  18.         int letra = palavra[i]-'a';
  19.        
  20.         if(current->word[letra] == NULL) current->word[letra] = new node();
  21.         current = current->word[letra];
  22.     } current->end = true;
  23. }
  24.  
  25. int main(){
  26.     string palavra; cin >> palavra;
  27.     init();
  28.     add(palavra);
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement