-
Notifications
You must be signed in to change notification settings - Fork 3
/
CCC10S2.java
48 lines (46 loc) · 1.53 KB
/
CCC10S2.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* CCC '10 S2 - Huffman Encoding
* Question type: String Algorithms
* 6/6 on DMOJ
* @author Tommy Pang
*/
public class CCC10S2 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static Map<String, String> map = new HashMap<>();
static int[] binary_length;
public static void main(String[] args) throws IOException{
int N = Integer.parseInt(br.readLine());
binary_length = new int[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
String letter = st.nextToken(), binary = st.nextToken();
binary_length[i] = binary.length();
map.put(binary, letter);
}
Arrays.sort(binary_length);
String code = br.readLine();
int i = 0;
StringBuilder ans = new StringBuilder();
while (true){
try {
for (int nxt : binary_length) {
String sub = code.substring(i, i+nxt);
if (map.containsKey(sub)) {
ans.append(map.get(sub));
i += nxt;
break;
}
}
}
catch (StringIndexOutOfBoundsException e){
System.out.println(ans);
return;
}
}
}
}