-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB1991.java
More file actions
84 lines (70 loc) · 2.08 KB
/
B1991.java
File metadata and controls
84 lines (70 loc) · 2.08 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package Practice;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class B1991 { //Æ®¸® ¼øÈ¸
static ArrayList<Integer>[] alpha;
static int[] parent;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
alpha = new ArrayList[27];
for(int i = 0; i < N; i++) alpha[i] = new ArrayList<>();
for(int i = 0; i < N; i++) {
String str = br.readLine();
int P = str.charAt(0) - 'A';
int L = str.charAt(2) - 'A';
int R = str.charAt(4) - 'A';
alpha[P].add(L);
alpha[P].add(R);
}
System.out.println(Solve1(0));
System.out.println(Solve2(0));
System.out.println(Solve3(0));
}
public static String Solve1(int P) {
String res = "" + (char)(P + 'A');
for(int a : alpha[P]) {
if(a == '.' - 'A') continue;
res += Solve1(a);
}
return res;
}
public static String Solve2(int P) {
String res = "";
if(alpha[P].get(0) != '.' - 'A' && alpha[P].get(1) != '.' - 'A') {
res += Solve2(alpha[P].get(0));
res += (char)(P + 'A');
res += Solve2(alpha[P].get(1));
}
else if(alpha[P].get(0) != '.' - 'A') {
res += Solve2(alpha[P].get(0));
res += (char)(P + 'A');
}
else if(alpha[P].get(1) != '.' - 'A') {
res += (char)(P + 'A');
res += Solve2(alpha[P].get(1));
}
if(alpha[P].get(0) == '.' - 'A' && alpha[P].get(1) == '.' - 'A') return res + (char)(P + 'A');
return res;
}
public static String Solve3(int P) {
String res = "";
if(alpha[P].get(0) != '.' - 'A' && alpha[P].get(1) != '.' - 'A') {
res += Solve3(alpha[P].get(0));
res += Solve3(alpha[P].get(1));
res += (char)(P + 'A');
}
else if(alpha[P].get(0) != '.' - 'A') {
res += Solve3(alpha[P].get(0));
res += (char)(P + 'A');
}
else if(alpha[P].get(1) != '.' - 'A') {
res += Solve3(alpha[P].get(1));
res += (char)(P + 'A');
}
if(alpha[P].get(0) == '.' - 'A' && alpha[P].get(1) == '.' - 'A') return res + (char)(P + 'A');
return res;
}
}