Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int flag=0;
  4. struct node
  5. {
  6. bool endmark;
  7. node *next[10];
  8. node()
  9. {
  10. endmark=false;
  11. for(int i=0; i<10; i++)
  12. next[i]=NULL;
  13. }
  14. }*root;
  15.  
  16. void insert(char *str,int len)
  17. {
  18. node *curr=root;
  19. for(int i=0; i<len; i++)
  20. {
  21. int id=str[i]-'0';
  22. if(curr->next[id]==NULL)
  23. curr->next[id]=new node();
  24. curr=curr->next[id];
  25. if(curr->endmark==1)
  26. flag=1;
  27. }
  28. curr->endmark=1;
  29. }
  30.  
  31.  
  32. void del(node *cur)
  33. {
  34. for(int i=0; i<10; i++) if(cur->next[i])
  35. del(cur->next[i]) ;
  36. delete(cur) ;
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.  
  43. root=new node();
  44. int num_word,j=1,T;
  45.  
  46. cin>>T;
  47.  
  48. while(T--)
  49. {
  50. flag=0;
  51. cin>>num_word;
  52.  
  53. for(int i=1; i<=num_word; i++)
  54. {
  55.  
  56. char str[50];
  57. scanf("%s",str);
  58. insert(str,strlen(str));
  59. }
  60. if(flag==1)
  61. cout<<"Case "<<j<<": NO"<<endl;
  62. else
  63. cout<<"Case "<<j<<": YES"<<endl;
  64. j++;
  65. del(root);
  66.  
  67. }
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement