aj30

Untitled

Mar 23rd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  6. const ll mod = 1e9+7;
  7.  
  8. struct trie{
  9. int cnt;
  10. bool eow;
  11. map<char,trie*> mp;
  12. trie(){
  13. cnt = 0;
  14. eow = false;
  15. }
  16. };
  17. void build(trie* root, string &s){
  18.  
  19. for(int i=0;i<s.length();++i){
  20. if(root->mp[s[i]] == NULL){
  21. root->mp[s[i]] = new trie();
  22. }
  23. root = root->mp[s[i]];
  24. ++root->cnt;
  25. }
  26. root->eow = true;
  27. }
  28. int ans = 0;
  29. int fun(trie* root, int k, int lvl){
  30.  
  31. if (root->cnt < k)
  32. return 0;
  33.  
  34. int f = 0;
  35. for(auto &p:root->mp){
  36. f+=fun(p.second,k,lvl+1);
  37. }
  38.  
  39.  
  40. if(root->cnt>=k){
  41. ans += lvl*((root->cnt-f)/k);
  42.  
  43. }
  44.  
  45. return f+k*((root->cnt-f)/k);
  46. }
  47. // void fun(trie* root, int k, int lvl){
  48. // // cout<<root->cnt<<endl;
  49. // if (root==NULL)
  50. // return ;
  51. // // cout<<"__"<<root->cnt<<endl;
  52. // int f = 0;
  53. // for(auto &p:root->mp){
  54. // fun(p.second,k,lvl+1);
  55. // }
  56. // // root->cnt -= f;
  57.  
  58. // if(root->cnt>=k){
  59. // ans += root->cnt/k;
  60. // // cout<<"__"<<root->cnt<<"->"<<lvl*(root->cnt/k)<<" f:"<<f<<endl;
  61. // }
  62. // }
  63. int g = 1;
  64. int solve(){
  65. ll n,k;cin>>n>>k;
  66. vector<string> a(n);
  67. for(int i=0;i<n;++i){
  68. cin>>a[i];
  69. }
  70. trie *root = new trie();
  71.  
  72. for(int i=0;i<n;++i){
  73. build(root,a[i]);
  74. }
  75. for(auto &p:root->mp){
  76. // cout<<"\n------------------\n";
  77. fun(p.second,k,1);
  78. }
  79. cout<<"Case #"<<g<<": "<<ans<<endl;
  80. ++g;
  81. return 0;
  82. }
  83. int main(){
  84. fast;
  85. int t=1;
  86. cin>>t;
  87. while(t--){
  88. solve();
  89. }
  90.  
  91. return 0;
  92. }
Add Comment
Please, Sign In to add comment