Advertisement
GlitchyTech

Tier

Dec 5th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define info(msg) cout << (#msg) << " = " << (msg) << endl;
  6. #define print(msg) cout << (msg) << endl;
  7. #define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var));
  8. #define pb push_back
  9. #define fr first
  10. #define sc second
  11. #define prev preww
  12.  
  13. using ll = long long;
  14. using ull = unsigned long long;
  15. using ld = long double;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using vi = vector<int>;
  19.  
  20. const int INF = 2e9;
  21. const ll LINF = 9e18;
  22. const int MOD1 = 2e9 + 11;
  23. const int MOD2 = 1e9 + 9;
  24. const int MOD3 = 1e9 + 7;
  25.  
  26. //program
  27.  
  28. struct Node {
  29.     Node(){
  30.         for (int i = 0; i <= 9; ++i) next[i] = nullptr;
  31.         isTerminal = false;
  32.     }
  33.  
  34.     Node *next[10];
  35.     bool isTerminal;
  36. };
  37.  
  38. bool is_the_last_one(Node *node){
  39.     Node head = *node;
  40.     for (int i = 0; i <= 9; ++i){
  41.         if (head.next[i] != nullptr) return false;
  42.     }
  43.     return true;
  44. }
  45.  
  46. bool doesnt_have_prefix(Node node){
  47.     for (int i = 0; i <= 9; ++i){
  48.         if (node.next[i] != nullptr) {
  49.             if (node.next[i]->isTerminal) {
  50.                 if (!is_the_last_one(node.next[i])) return false;
  51.             }
  52.             doesnt_have_prefix(*node.next[i]);
  53.         }
  54.     }
  55.     return true;
  56. }
  57.  
  58. void output_trie(Node head, int lvl = 0){
  59.     cout << lvl << " : \n";
  60.     for (int i = 0; i <= 9; ++i){
  61.         if (head.next[i] != nullptr) cout << i << ' ';
  62.     }
  63.     cout << endl;
  64.  
  65.     for (int i = 0; i <= 9; ++i){
  66.         if (head.next[i] != nullptr) output_trie(*head.next[i], lvl + 1);
  67.     }
  68. }
  69.  
  70. int main(){
  71.     //freopen("input.txt", "r', stdin);
  72.     //freopen("output.txt", "w", stdout);
  73.     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  74.     int t = 0;
  75.     cin >> t;
  76.     vector<string> ans;
  77.     for (int i = 0; i < t; ++i){
  78.         int n = 0;
  79.         cin >> n;
  80.         Node Head;
  81.  
  82.  
  83.         //Build Trie
  84.         for (int j = 0; j < n; ++j){
  85.             string s = "";
  86.             cin >> s;
  87.             int len_s = s.size();
  88.             Node cur_head = Head;
  89.  
  90.             for (int k = 0; k < len_s; ++k){
  91.                 int p = s[k] - '0';
  92.                 if (cur_head.next[p] == nullptr){
  93.                     cur_head.next[p] = new Node;
  94.                     if (k == len_s - 1) cur_head.next[p]->isTerminal = true;
  95.                     cur_head = *cur_head.next[p];
  96.                 }
  97.             }
  98.         }
  99.  
  100.         //Output of trie. Why empty?
  101.         //output_trie(Head);
  102.         //Algorithm
  103.         if (!doesnt_have_prefix(Head)) ans.pb("NO");
  104.         else ans.pb("YES");
  105.     }
  106.  
  107.     for (auto elem : ans) cout << elem << endl;
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement