Advertisement
_rashed

SPOJ PHONELST

Jul 11th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. class Trie {
  9.     Trie * c[10];
  10.     bool flag;
  11.  
  12.     public:
  13.     Trie() {
  14.         memset(c,0,sizeof(c));
  15.         flag = 0;
  16.     }
  17.  
  18.     ~Trie() {
  19.         for(int i = 0; i < 10; i++) {
  20.             if(c[i]) {
  21.                 delete c[i];
  22.             }
  23.  
  24.         }
  25.     }
  26.  
  27.     bool add(string &s, int idx = 0, bool cn = 0) {
  28.         if(flag) {
  29.             return false;
  30.         }
  31.         if(idx == s.size()) {
  32.             flag = 1;
  33.             return cn;
  34.         }
  35.         int curr = s[idx]-'0';
  36.         if(c[curr] == 0) {
  37.             c[curr] = new Trie();
  38.             cn = 1;
  39.         }
  40.         return c[curr]->add(s,idx+1, cn);
  41.     }
  42. };
  43.  
  44. int main()
  45. {
  46.     ios_base::sync_with_stdio(false);
  47.     cin.tie(NULL);
  48.     cout.tie(NULL);
  49.     int t;
  50.     cin >> t;
  51.     while(t--) {
  52.         int n;
  53.         cin >> n;
  54.         Trie dict;
  55.         bool con = true;
  56.         for(int i = 0; i < n; i++) {
  57.             string s;
  58.             cin >> s;
  59.             if(!dict.add(s)) {
  60.                 con = false;
  61.             }
  62.         }
  63.         cout << (con ? "YES\n":"NO\n");
  64.     }
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement