fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(false);
  6. int t;
  7. cin>>t;
  8. int u=1;
  9. while(t--)
  10. {
  11. int n,k,maxm=0;
  12. cin>>n>>k;
  13. cout<<"Case "<<u++<<":"<<endl;
  14. unordered_map<string ,int> m;
  15. queue<string> q;
  16. set<string> v[n+1];
  17. string s,l,p,prev;
  18. for(int i=0;i<n;i++)
  19. {
  20. cin>>s;
  21. if(q.size()>=k)
  22. {
  23. p=q.front();
  24. q.pop();
  25. v[m[p]].erase(p);
  26. m[p]--;
  27. if(maxm==m[p]+1)
  28. {
  29. if(v[maxm].size()==0)
  30. maxm=m[p];
  31. }
  32.  
  33. v[m[p]].insert(p);
  34.  
  35. }
  36. q.push(s);
  37. m[s]++;
  38. v[m[s]].insert(s);
  39. if(m[s]>maxm)
  40. {
  41. l=s;
  42. maxm=m[s];
  43. }
  44. if(m[p]>maxm)
  45. {
  46. maxm=m[p];
  47. l=p;
  48. }
  49.  
  50. set<string>::iterator it=v[maxm].begin();
  51. cout<<*it<<" "<<maxm<<endl;
  52. }
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 3476KB
stdin
2
8 3
hello
hi
who
hi
hi
hello
who
when
8 3
hello
hi
who
hi
hi
hello
who
when
stdout
Case 1:
hello 1
hello 1
hello 1
hi 2
hi 2
hi 2
hello 1
hello 1
Case 2:
hello 1
hello 1
hello 1
hi 2
hi 2
hi 2
hello 1
hello 1