/
dwite201111p3.java
58 lines (47 loc) · 1.47 KB
/
dwite201111p3.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
/*
* DWITE programming contest solutions
* November 2011 - Problem 3: "Anonymous Shopping"
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/dwite-programming-contest-solutions
* https://github.com/nayuki/DWITE-programming-contest-solutions
*/
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public final class dwite201111p3 extends DwiteSolution {
public static void main(String[] args) {
new dwite201111p3().run("DATA3.txt", "OUT3.txt");
}
protected void runOnce() {
Map<String,Set<String>> personalInfoToDrinks = new HashMap<>();
io.tokenizeLine();
int n = io.readIntToken();
int m = io.readIntToken();
// Read and process store records
for (int i = 0; i < n; i++) {
io.tokenizeLine();
io.readToken(); // Discard store ID
String personalInfo = io.readToken();
String drink = io.readToken();
get(personalInfoToDrinks, personalInfo, new HashSet<String>()).add(drink);
}
// Read and process school records
int count = 0;
for (int i = 0; i < m; i++) {
io.tokenizeLine();
io.readToken(); // Discard name
String personalInfo = io.readToken();
Set<String> drinks = get(personalInfoToDrinks, personalInfo, new HashSet<String>());
if (drinks.size() == 1)
count++;
}
io.println(count);
}
private static <K,V> V get(Map<K,V> map, K key, V defaultVal) {
if (!map.containsKey(key))
map.put(key, defaultVal);
return map.get(key);
}
}