Advertisement
Saleh127

Light OJ 1129 / Trie Basic

Feb 22nd, 2023
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. /***
  2.  created: 2023-02-23-00.15.00
  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. ll ans=0;
  17. ll tr[100005][11],v[100005],id=0;
  18. void insert1(string &a)
  19. {
  20.     ll now=0;
  21.     for(ll i=0;a[i];i++)
  22.     {
  23.         if(tr[now][(a[i]-'0')]==0)
  24.         {
  25.             tr[now][(a[i]-'0')]=++id;
  26.         }
  27.         else if(tr[now][(a[i]-'0')] && i==a.size()-1)
  28.         {
  29.             ans=0;
  30.         }
  31.         now=tr[now][(a[i]-'0')];
  32.         if(v[now])
  33.         {
  34.             ans=0;
  35.         }
  36.     }
  37.     v[now]=1;
  38. }
  39.  
  40. int main()
  41. {
  42.     ios_base::sync_with_stdio(0);
  43.     cin.tie(0);
  44.     cout.tie(0);
  45.  
  46.  
  47.     test
  48.     {
  49.         ll n,m,i,j,k,l;
  50.         cin>>n;
  51.         string a;
  52.         ans=1;
  53.         id=0;
  54.         memset(tr,0,sizeof tr);
  55.         memset(v,0,sizeof v);
  56.  
  57.         for(i=0;i<n;i++)
  58.         {
  59.             cin>>a;
  60.             insert1(a);
  61.         }
  62.  
  63.         cout<<"Case "<<cs<<": ";
  64.         if(ans) cout<<"YES"<<nl;
  65.         else cout<<"NO"<<nl;
  66.     }
  67.  
  68.  
  69.     return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement