-
Notifications
You must be signed in to change notification settings - Fork 0
/
VasyaAndMultisets.java
76 lines (73 loc) · 2.12 KB
/
VasyaAndMultisets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* https://codeforces.com/problemset/problem/1051/C #implementation #todo
*
* <p>cnt[x] cntNice = 0
*
* <p>for x in a: if (!cnt.containsKey(x)): cntNice++; cnt.set(x, 1); else: temp = cnt.get(x)
* cnt.set(x, temp + 1) if temp==1: cntNice--; needNewNice = false if cntNice % 2 == 1: needNewNice
* = true
*
* <p>flag = false for key in cnt: if cnt.get(key) > 2: flag = true break if !flag: return FALSE
* subset = 'A' for x in a: if cnt.get(x) == 1: print subset subset = 'A' + 'B' - subset else: if
* needNewNice && cnt.get(x) > 2: print 'B' needNewNice = false else: print 'A'
*/
import java.util.HashMap;
import java.util.Scanner;
public class VasyaAndMultisets {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
int niceNumbers = 0;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
int k = arr[i];
if (map.containsKey(k)) {
int v = map.get(k);
map.replace(k, v + 1);
if (v == 1) {
niceNumbers -= 1;
}
} else {
niceNumbers += 1;
map.put(k, 1);
}
}
// check need to create newNiceNumber
boolean needNiceNumber = false;
if (niceNumbers % 2 == 1) {
needNiceNumber = true;
// check whether we can make nice number or not
boolean canMakeNiceNumber = false;
for (int k : map.keySet()) {
if (map.get(k) > 2) {
canMakeNiceNumber = true;
break;
}
}
if (!canMakeNiceNumber) {
System.out.println("NO");
return;
}
}
// print result
System.out.println("YES");
char subSet = 'A';
for (int k : arr) {
if (map.get(k) == 1) {
System.out.print(subSet);
// change subset
subSet = (char) ('A' + 'B' - subSet);
} else {
if (needNiceNumber && map.get(k) > 2) {
// ood niceNumbers => always in subset B
System.out.print('B');
needNiceNumber = false;
} else {
System.out.print('A');
}
}
}
}
}