Guest User

Untitled

a guest
Apr 13th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. #define FILEIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
  5.  
  6. const int N=5e5+99;
  7.  
  8. struct node
  9. {
  10. struct node* nxt[26];
  11. int cnt=0;
  12. };
  13. node* head=new node();
  14. void clear1(node *nd)
  15. {
  16. for(int i=0;i<26;i++)
  17. if(nd->nxt[i]!=NULL) clear1(nd->nxt[i]);
  18. free(nd);
  19. }
  20. void insert(string s)
  21. {
  22. node* nd=head;
  23. for(int i=0;i<s.length();i++)
  24. {
  25. int k=s[i]-'A';
  26. if(nd->nxt[k]==NULL)nd->nxt[k]=new node();
  27. nd=nd->nxt[k];
  28. nd->cnt++;
  29. }
  30. }
  31. int ans=0;
  32. int dfs(node* nd)
  33. {
  34. int k=0;
  35. for(int i=0;i<26;i++)
  36. {
  37. if(nd->nxt[i]==NULL) continue;
  38. k+=dfs(nd->nxt[i]);
  39. }
  40.  
  41. int kk=(nd->cnt)-k;
  42.  
  43. if(kk>=2) k+=2,ans+=2;
  44. return k;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. int32_t main()
  51. {
  52.  
  53.  
  54.  
  55. #ifndef ONLINE_JUDGE
  56. FILEIO
  57. #endif
  58.  
  59. int t0;cin>>t0;
  60.  
  61. for( int tc=1;tc<=t0;tc++)
  62. {
  63. clear1(head);
  64. head=new node();
  65. int n; ans=0;
  66. cin>>n;
  67. for(int i=0;i<n;i++)
  68. {
  69. string s;cin>>s;
  70. reverse(s.begin(),s.end());
  71. insert(s);
  72. }
  73. dfs(head);
  74. cout<<"Case #"<<tc<<": "<<ans<<endl;
  75.  
  76. }
  77.  
  78. }
Add Comment
Please, Sign In to add comment