fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool check (priority_queue<pair<int, char>> &pq, string &s, int d){
  5. int soViTriConLai = s.length();
  6.  
  7. while(!pq.empty()){
  8. pair<int, char> pr = pq.top();
  9. pq.pop();
  10.  
  11. if(s.length() % 2 != 0){
  12. if(pr.first > s.length() / d + 1) return false;
  13. }
  14. else{
  15. if(pr.first > s.length() / d) return false;
  16. }
  17. soViTriConLai -= pr.first;
  18. }
  19.  
  20. return true;
  21. }
  22.  
  23. int main(){
  24. int t; cin >> t;
  25. while(t--){
  26. int d; cin >> d;
  27. string s; cin >> s;
  28.  
  29. unordered_map<char, int> ump;
  30. for(char c : s)
  31. ++ump[c];
  32.  
  33. priority_queue<pair<int, char>> pq;
  34. for(auto pr : ump)
  35. pq.push({pr.second, pr.first});
  36.  
  37. if(check(pq, s, d)) cout << "1\n";
  38. else cout << "-1\n";
  39. }
  40. }
Success #stdin #stdout 0s 5308KB
stdin
1
7
aaabbcc
stdout
-1