fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define mp make_pair
  5. #define nline cout<<"\n"
  6. #define fast ios_base::sync_with_stdio(false),cin.tie(0)
  7. #define ain(A, B, C) assert(IN(A, B, C))
  8. #define ull unsigned long long int
  9. #define ll long long int
  10. #define pii pair<int,int>
  11. #define MAXX 100009
  12. #define fr(a,b,i) for(int i=a;i<b;i++)
  13. vector<int>G[MAXX];
  14. struct node
  15. {
  16. bool leaf;
  17. struct node* child[10];
  18. node()
  19. {
  20. for(int i=0;i<10;i++)child[i]=NULL;
  21. leaf=false;
  22. }
  23. };
  24. struct node* root;
  25. bool add(string &s)
  26. {
  27. struct node* curr=root;
  28. bool result = false;
  29. for(int i=0;i<s.length();i++)
  30. {
  31. if (curr->leaf) {
  32. result = true;
  33. }
  34. int SS=s[i]-'0';
  35. if(curr->child[SS]==NULL)
  36. curr->child[SS]=new node();
  37. curr=curr->child[SS];
  38. }
  39. for(int i=0;i<10;i++)
  40. {
  41. if(curr->child[i]!=NULL)
  42. return true;
  43. }
  44.  
  45. if (curr->leaf || result) {
  46. return true;
  47. }
  48.  
  49. curr->leaf=true;
  50. return false;
  51. }
  52. void deleteme(node* curr)
  53. {
  54. for(int i=0;i<10;i++)
  55. if(curr->child[i])
  56. deleteme(curr->child[i]);
  57. delete curr;
  58. }
  59. int main()
  60. {
  61. fast;
  62. int t;
  63. cin>>t;
  64. while(t--)
  65. {
  66. root=new node();
  67. int n,f=0;
  68. cin>>n;
  69. for(int i=0;i<n;i++)
  70. {
  71. string s;
  72. cin>>s;
  73. if(add(s)){f=1;}
  74. }
  75. if(f)
  76. cout<<"NO"<<endl;
  77. else
  78. cout<<"YES"<<endl;
  79. deleteme(root);
  80. }
  81. return 0;
  82. }
Success #stdin #stdout 0s 4632KB
stdin
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
stdout
NO
YES