-
Notifications
You must be signed in to change notification settings - Fork 6
/
Seonghun.java
62 lines (52 loc) · 1.5 KB
/
Seonghun.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Seonghun {
static int N;
static Set<String> roots = new TreeSet<>(); // 루트 디렉토리 저장
static Map<String, Set<String>> tree = new TreeMap<>(); // 디렉토리 트리
static StringBuilder answer = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// initialize
N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
String path = br.readLine();
String[] dirs = path.split("\\\\");
String key = dirs[0];
// 루트 디렉토리 저장
roots.add(key);
// 디렉토리 트리 형성
for (int j = 0; j < dirs.length - 1; j++) {
if (!tree.containsKey(key)) {
tree.put(key, new TreeSet<>());
}
String value = key + "\\" + dirs[j + 1];
tree.get(key).add(value);
key = value;
}
}
// solution
for (String root : roots) {
printDir(0, root);
}
// result
System.out.println(answer);
}
static void printDir(int depth, String dir) {
for (int d = 0; d < depth; d++) {
answer.append(" ");
}
int idx = dir.lastIndexOf("\\");
answer.append(dir.substring(idx == -1 ? 0 : idx + 1)).append("\n");
if (tree.containsKey(dir)) {
for (String subdir : tree.get(dir)) {
printDir(depth + 1, subdir);
}
}
}
}