Saleh127

UVA 11362 / Trie Basic

Jan 18th, 2023
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. /***
  2.  created: 2023-01-19-00.43.24
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define all(we) we.begin(),we.end()
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define nl '\n'
  15.  
  16.  
  17. class Node{
  18. public:
  19.     bool endd;
  20.     Node *child[15];
  21.     Node()
  22.     {
  23.         endd=false;
  24.         for(ll i=0;i<13;i++) child[i]=NULL;
  25.     }
  26. };
  27.  
  28. void Insert(Node *cur,string a)
  29. {
  30.     ll n=a.size();
  31.     for(ll i=0;i<n;i++)
  32.     {
  33.         if(cur->child[a[i]-'0']==NULL)
  34.         {
  35.             cur->child[a[i]-'0'] = new Node();
  36.         }
  37.         cur = cur->child[a[i]-'0'];
  38.     }
  39.     cur->endd=true;
  40. }
  41.  
  42. bool Search(Node *cur, string a,ll ind)
  43. {
  44.     if(ind==a.size()) return 1;
  45.     if(cur->endd==true) return 0;
  46.     return Search(cur->child[a[ind]-'0'],a,ind+1);
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52.     ios_base::sync_with_stdio(0);
  53.     cin.tie(0);
  54.     cout.tie(0);
  55.  
  56.  
  57.     test
  58.     {
  59.         ll n,m,i,j,k,l;
  60.  
  61.         cin>>n;
  62.  
  63.         string a[n+4];
  64.  
  65.         Node *root = new Node();
  66.  
  67.         for(i=0;i<n;i++)
  68.         {
  69.             cin>>a[i];
  70.             Insert(root,a[i]);
  71.         }
  72.         l=0;
  73.  
  74.         for(i=0;i<n;i++)
  75.         {
  76.             if(!Search(root,a[i],0))
  77.             {
  78.                 l=1;
  79.                 break;
  80.             }
  81.         }
  82.  
  83.         if(l) cout<<"NO"<<nl;
  84.         else cout<<"YES"<<nl;
  85.     }
  86.  
  87.  
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment