fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define MOD 1000000007
  4. #define MAX 100005
  5. #define ll long long
  6. #define slld(t) scanf("%lld",&t)
  7. #define sd(t) scanf("%d",&t)
  8. #define pd(t) printf("%d\n",t)
  9. #define plld(t) printf("%lld\n",t)
  10. #define pcc pair<char,char>
  11. #define pii pair<int,int>
  12. #define pll pair<ll,ll>
  13. #define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
  14. #define mp(a,b) make_pair(a,b)
  15. #define FF first
  16. #define SS second
  17. #define pb(x) push_back(x)
  18. #define vi vector<int>
  19. #define clr(x) memset(x,0,sizeof(x))
  20.  
  21. using namespace std;
  22.  
  23. char s[MAX];
  24. vi curr;
  25. int a[28];
  26.  
  27. int main(){
  28. int t;
  29. sd(t);
  30. while(t--){
  31. scanf("%s",s);
  32. int k;sd(k);
  33.  
  34. clr(a);
  35.  
  36. int l=strlen(s);
  37.  
  38. for(int i=0;i<l;i++){
  39. a[s[i]-'a']++;
  40. }
  41.  
  42. curr.clear();
  43.  
  44. for(int i=0;i<28;i++){
  45. if(a[i]) curr.pb(a[i]);
  46. }
  47.  
  48. sort(curr.begin(),curr.end());
  49.  
  50. int ans=MOD;
  51.  
  52. for(int i=0;i<curr.size();i++){
  53. int mn=curr[i];
  54. int now=0;
  55. for(int j=0;j<i;j++){
  56. now+=curr[j];
  57. }
  58. for(int j=i+1;j<curr.size();j++){
  59. if(curr[j]>mn+k){
  60. now+=curr[j]-(mn+k);
  61. }
  62. }
  63. ans=min(ans,now);
  64. }
  65.  
  66. pd(ans);
  67. }
  68. }
Success #stdin #stdout 0s 3560KB
stdin
1
aaabbbcccdddeeee 0
stdout
1