-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
74 lines (67 loc) · 2.48 KB
/
Solution.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
package com.algorithm.playground.google.codejam._2019.round1.c.task2;
import java.util.*;
import java.util.Map.Entry;
/**
* https://codingcompetitions.withgoogle.com/codejam/round/00000000000516b9/0000000000134e91#
*/
public class Solution {
private static Scanner console;
public static void main(String[] args) {
try (Scanner console = new Scanner(System.in)) {
Solution.console = console;
int tests = console.nextInt();
//skip number oa requests
console.nextInt();
for (int test = 1; test <= tests; test++) {
String ans = solve();
System.out.println(ans);
if (console.next().equals("N")) {
throw new RuntimeException("WA");
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
private static String solve() {
char[] ans = new char[5];
List<Integer> requests = new ArrayList<>();
for (int i = 0; i < 595; i += 5) {
requests.add(i);
}
Set<Character> expected = new HashSet<>(Arrays.asList('A', 'B', 'C', 'D', 'E'));
for (int i = 0; i < 4; i++) {
Entry<Character, List<Integer>> missing = getMissing(requests);
expected.remove(missing.getKey());
if (i != 3) {
ans[i] = missing.getKey();
requests = missing.getValue();
} else {
ans[3] = expected.iterator().next();
ans[4] = missing.getKey();
}
}
return new String(ans);
}
private static Entry<Character, List<Integer>> getMissing(List<Integer> requests) {
Map<Character, List<Integer>> positions = new HashMap<>();
for (Integer i : requests) {
System.out.println(i + 1);
Character response = console.next().charAt(0);
if (!positions.containsKey(response)) {
List<Integer> list = new ArrayList<>();
list.add(i + 1);
positions.put(response, list);
} else {
positions.get(response).add(i + 1);
}
}
Entry<Character, List<Integer>> missing = null;
for (Entry<Character, List<Integer>> entry : positions.entrySet()) {
if (missing == null || missing.getValue().size() > entry.getValue().size()) {
missing = entry;
}
}
return missing;
}
}