-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddingWords.java
68 lines (55 loc) · 2.1 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
/*
Problem: https://open.kattis.com/problems/addingwords
Author: Adrian Reithaug
Submitted: June 4th, 2017
Time: 0.27s / 5.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class AddingWords {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Map<Integer, String> numToStr = new HashMap<>();
Map<String, Integer> StrToNum = new HashMap<>();
String line = reader.readLine();
while (line != null) {
String[] data = line.split(" ");
if (data[0].equals("clear")) {
numToStr = new HashMap<>();
StrToNum = new HashMap<>();
} else if (data[0].equals("def")) {
int newValue = Integer.parseInt(data[2]);
numToStr.remove(StrToNum.get(data[1]));
numToStr.put(newValue, data[1]);
StrToNum.put(data[1], newValue);
} else if (data[0].equals("calc")) {
int sum = 0;
boolean unknown = false;
char operator = '+';
for (int i = 1; i < data.length - 1; i++) {
if (data[i].equals("+")) {
operator = '+';
continue;
}
if (data[i].equals("-")) {
operator = '-';
continue;
}
if (StrToNum.containsKey(data[i])) {
int value = StrToNum.get(data[i]);
sum += (operator == '+') ? value : -value;
} else {
unknown = true;
break;
}
}
String result = (!unknown && numToStr.containsKey(sum)) ? numToStr.get(sum) : "unknown";
System.out.println(line.substring(5) + " " + result);
}
line = reader.readLine();
}
}
}