|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + private static int N; |
| 7 | + public static void main(String[] args) throws Exception { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + |
| 10 | + N = Integer.parseInt(br.readLine()); |
| 11 | + |
| 12 | + File root = File.from("root"); |
| 13 | + for (int i = 0; i < N; i++) { |
| 14 | + root.add(br.readLine().split("\\\\")); |
| 15 | + } |
| 16 | + |
| 17 | + root.printTree(); |
| 18 | + } |
| 19 | + |
| 20 | + private static class File implements Comparable<File> { |
| 21 | + private final String name; |
| 22 | + private final Set<File> files; |
| 23 | + |
| 24 | + private File(String name) { |
| 25 | + this.name = name; |
| 26 | + files = new TreeSet<>(); |
| 27 | + } |
| 28 | + |
| 29 | + public static File from(String name) { |
| 30 | + return new File(name); |
| 31 | + } |
| 32 | + |
| 33 | + public void add(String[] names) { |
| 34 | + add(names, 0); |
| 35 | + } |
| 36 | + |
| 37 | + public void add(String[] names, int index) { |
| 38 | + if (names.length == index) return; |
| 39 | + String name = names[index]; |
| 40 | + files.add(File.from(name)); |
| 41 | + |
| 42 | + for (File file : files) { |
| 43 | + if (file.isSameName(name)) { |
| 44 | + file.add(names, index + 1); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public boolean isSameName(String name) { |
| 50 | + return this.name.equals(name); |
| 51 | + } |
| 52 | + |
| 53 | + public Set<File> getFiles() { |
| 54 | + return files; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public void printTree() { |
| 59 | + printTree(0); |
| 60 | + } |
| 61 | + |
| 62 | + public void printTree(int tabCount) { |
| 63 | + if (files.isEmpty()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + for (File file : files) { |
| 68 | + for (int i = 0; i < tabCount; i++) { |
| 69 | + System.out.print(" "); |
| 70 | + } |
| 71 | + System.out.println(file); |
| 72 | + |
| 73 | + file.printTree(tabCount + 1); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String toString() { |
| 79 | + return name; |
| 80 | + } |
| 81 | + |
| 82 | + // 파일이름에 대해서만 |
| 83 | + @Override |
| 84 | + public int hashCode() { |
| 85 | + return Objects.hash(name); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean equals(Object obj) { |
| 90 | + if (this == obj) |
| 91 | + return true; |
| 92 | + if (obj == null) |
| 93 | + return false; |
| 94 | + if (getClass() != obj.getClass()) |
| 95 | + return false; |
| 96 | + File other = (File) obj; |
| 97 | + return Objects.equals(name, other.name); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int compareTo(File o) { |
| 102 | + return this.name.compareTo(o.toString()); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments