-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddingWords.java
68 lines (60 loc) · 2.06 KB
/
AddingWords.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
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.*;
import java.util.stream.*;
import java.io.*;
import java.math.*;
import java.text.*;
/*
* @author Theodoric Keith Lim
*/
class AddingWords {
public static void main(String[] args) {
new AddingWords();
}
AddingWords() {
answer();
}
void answer() {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
Map<String, Integer> hmap = new HashMap<>();
Map<Integer, String> hmap2 = new HashMap<>();
while (sc.hasNextLine()) {
String s = sc.nextLine();
String[] tmp = s.split("\\s+");
if (tmp[0].equals("def")) {
hmap2.remove(hmap.get(tmp[1]));
hmap.put(tmp[1], Integer.parseInt(tmp[2]));
hmap2.put(Integer.parseInt(tmp[2]), tmp[1]);
} else if (tmp[0].equals("calc")) {
int total = 0;
if (hmap.containsKey(tmp[1])) {
total += hmap.get(tmp[1]);
for (int i = 2; i < tmp.length - 1; i += 2) {
if (hmap.containsKey(tmp[i + 1])) {
int a = hmap.get(tmp[i + 1]);
if (tmp[i].equals("+")) {
total += a;
} else total -= a;
} else {
total = Integer.MIN_VALUE;
break;
}
}
} else {
total = Integer.MIN_VALUE;
}
sb.append(s.substring(5, s.length()));
sb.append(" ");
sb.append(hmap2.containsKey(total) ? hmap2.get(total) : "unknown");
sb.append("\n");
} else if (tmp[0].equals("clear")) {
hmap.clear();
hmap2.clear();
}
}
System.out.println(sb.toString());
sc.close();
}
}