fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. bool endmark;
  5. int cnt_trees;
  6. node *next[11];
  7. node(){
  8. endmark=false;
  9. cnt_trees=0;
  10. for(int i=0;i<11;i++) next[i]=NULL;
  11. }
  12. }*root;
  13.  
  14. bool insert_into_Trie(string str){
  15. node *cur=root;
  16. for(int i=0;str[i];i++){
  17. int id=str[i]-'a';
  18. if(i==(str.size())-1 && cur->next[id]!=NULL ) return false;
  19. if(cur->next[id]==NULL)
  20. cur->next[id]=new node();
  21. cur=cur->next[id];
  22. if(cur->endmark) return false;
  23. }
  24. cur->endmark=true;
  25. return true;
  26. }
  27.  
  28. int main()
  29. {
  30. int n,f=1;
  31. string str,bad;
  32. bool ok=true;
  33. cin>>n;
  34. root=new node();
  35. while(n--){
  36. cin>>str;
  37. if(!ok) continue;
  38. ok=insert_into_Trie(str);
  39. if(!ok) bad=str;
  40. }
  41. if(ok) cout<<"GOOD SET\n";
  42. else cout<<"BAD SET\n"<<bad<<endl;
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 16064KB
stdin
7
aab
defgab
abcde
aabcde
cedaaa
bbbbbbbbbb
jabjjjad
stdout
BAD SET
aabcde