Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define forsn(i, s, n) for(int i=s;i<int(n);i++)
  6. #define forn(i, n) forsn(i, 0, n)
  7. #define all(v) v.begin(), v.end()
  8. #define rall(v) v.rbegin(), v.rend()
  9. #define NACHO ios_base::sync_with_stdio(0);cin.tie(NULL);
  10.  
  11. typedef long long tint;
  12.  
  13. struct Trie{
  14.     map<char, Trie> arbol;
  15.     bool finPalabra = false;
  16. };
  17.  
  18. bool encontrado = 1;
  19. string s;
  20. int tamPalabra;
  21.  
  22. void insertar(Trie &t, int pos) {
  23.     if(pos < tamPalabra){
  24.         if(pos == tamPalabra-1) t.finPalabra = true;
  25.         insertar(t.arbol[s[pos]], pos+1);
  26.     }
  27. }
  28.  
  29. bool consistent = 1;
  30.  
  31. void recorrer(Trie t, string palabra){
  32.     if(t.finPalabra && t.arbol.size() > 0){ consistent = 0; cout << palabra << endl;}
  33.     for(auto &i:t.arbol) {
  34.         recorrer(i.second, palabra+i.first);
  35.     }
  36. }
  37.  
  38. int main(){
  39.     int n; cin >> n;
  40.     Trie t;
  41.     vector<string> strs (n);
  42.     forn(i, n){
  43.         cin >> s;
  44.         strs[i] = s;
  45.         tamPalabra = int(s.size())+1;
  46.         insertar(t, 0);
  47.     }
  48.     recorrer(t, "");
  49.     cout << consistent << "\n";
  50. }
  51. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement