Advertisement
Imran1107048

D - Singhal and Equality

Oct 30th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define endl "\n"
  6. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7.  
  8. void solve();
  9. int32_t main()
  10. {
  11. IOS;
  12. cout << fixed << setprecision(10);
  13. int _ = 1;
  14. cin >> _;
  15. while(_--) solve();
  16. return 0;
  17. }
  18.  
  19. void solve()
  20. {
  21. string s;
  22. cin >> s;
  23. int n = s.size();
  24. int a[26] = {0};
  25. for(int i=0;i<n;i++)
  26. a[s[i] - 'a']++;
  27.  
  28. sort(a,a+26, greater<int>());
  29.  
  30. int cnt = n;
  31. for(int i=1;i<=26;i++){
  32. if(n%i==0){
  33. int x = n/i;
  34. int y = 0;
  35. for(int j=0;j<i;j++){
  36. y += min(a[j],x);
  37. }
  38. cnt = min(cnt,n-y);
  39. }
  40. }
  41. cout << cnt << endl;
  42. }
  43.  
  44.  
  45. //int mn = a[0];
  46. // for(int i=1;i<26;i++)
  47. // mn = max(mn,a[i]);
  48. // int cnt = 0, cnt2 = 0, cntt = 0;
  49. // for(int i=0;i<26;i++){
  50. // if(a[i]>0 && a[i]!=mn){
  51. // cnt++;
  52. // cntt = max(cntt, abs(mn-a[i]));
  53. // }
  54. // if(a[i] == mn)
  55. // cnt2++;
  56. // }
  57. // if(cntt > 1){
  58. // cout << cntt << endl;
  59. // return;
  60. // }
  61. // cnt++;
  62. // cout << cnt/2 << endl;
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement