fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int n;
  6. cin >> n;
  7. vector<int> a(n);
  8. map<int, int> count;
  9. for (int i = 0; i < n; ++i) {
  10. cin >> a[i];
  11. count[a[i]]++;
  12. }
  13.  
  14. // Tìm số nguyên không âm nhỏ nhất xuất hiện < 3 lần
  15. int v = 0;
  16. while (count[v] >= 3) {
  17. v++;
  18. }
  19.  
  20. // Theo dõi số lượng đã phân phối cho A, B, C
  21. map<int, int> distributed;
  22. string s(n, ' ');
  23.  
  24. for (int i = 0; i < n; ++i) {
  25. int val = a[i];
  26. if (val < v) {
  27. // Phân phối lần lượt vào A, B, C
  28. if (distributed[val] == 0) {
  29. s[i] = 'A';
  30. } else if (distributed[val] == 1) {
  31. s[i] = 'B';
  32. } else {
  33. s[i] = 'C';
  34. }
  35. distributed[val]++;
  36. } else {
  37. // Các phần tử từ v trở lên cho vào A hết cho đơn giản
  38. s[i] = 'A';
  39. }
  40. }
  41.  
  42. cout << "YES\n";
  43. cout << s << "\n";
  44. }
  45.  
  46. int main() {
  47. ios_base::sync_with_stdio(false);
  48. cin.tie(NULL);
  49. int t;
  50. cin >> t;
  51. while (t--) {
  52. solve();
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5280KB
stdin
5
6
1 0 0 1 2 1
4
0 0 0 0
3
0 2 2
4
6 7 6 7
5
0 0 0 1 2
stdout
YES
AAAAAA
YES
ABCC
YES
AAA
YES
AAAA
YES
ABCAA