-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathp266.java
52 lines (40 loc) · 1.32 KB
/
p266.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
import java.util.*;
public class p266 {
public static void main(String[] args) {
final Scanner s = new Scanner(System.in);
char[][] m;
int f, c;
char replace1, replace2;
String input;
TreeMap<Character, Character> map = new TreeMap<>();
while (true) {
f = s.nextInt();
c = s.nextInt();
if (f == 0 && c == 0) break;
m = new char[f][c];
for (int i = 0; i < f; i++) {
input = s.next();
for (int j = 0; j < c; j++) {
m[i][j] = input.charAt(j);
map.put(m[i][j], m[i][j]);
}
}
for (int i = s.nextInt(); i > 0; i--) {
replace1 = s.next().charAt(0);
replace2 = s.next().charAt(0);
for (Map.Entry<Character, Character> entry : map.entrySet()) {
if (entry.getValue() == replace1) {
map.put(entry.getKey(), replace2);
}
}
}
for (int i = 0; i < f; i++) {
for (int j = 0; j < c; j++) {
System.out.print(map.get(m[i][j]));
}
System.out.println();
}
map.clear();
}
}
}