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((pr.first - 1) * d + 1 > s.length()) return false;
  12. }
  13.  
  14. return true;
  15. }
  16.  
  17. int main(){
  18. int t; cin >> t;
  19. while(t--){
  20. int d; cin >> d;
  21. string s; cin >> s;
  22.  
  23. unordered_map<char, int> ump;
  24. for(char c : s)
  25. ++ump[c];
  26.  
  27. priority_queue<pair<int, char>> pq;
  28. for(auto pr : ump)
  29. pq.push({pr.second, pr.first});
  30.  
  31. if(check(pq, s, d)) cout << "1\n";
  32. else cout << "-1\n";
  33. }
  34. }
Success #stdin #stdout 0s 5320KB
stdin
2
2
abb
2
aaa
stdout
1
-1