CodeForce158C - Linux 터미널 cd and pwd
이 문제는 리눅스 터미널의 cd와 pwd를 구현하는 문제로 linkedList를 이용하는 문제이다.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import java.lang.reflect.Array; import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; public class CodeForce158C { static boolean[][] nodeTable = new boolean[505][505]; static String[] nodeString = new String[505]; static int currentNode = 0, createdNode; static boolean[] visited = new boolean[505]; static String[] pathDirectory = new String[505]; static int pathNode = 0; static boolean flag; public static void main(String[] args) { int n; nodeTable[0][0] = true; Scanner sc = new Scanner(System.in); n = sc.nextInt(); String commandLine = ""; String command = ""; String directoryList = ""; String[] directorys = new String[505]; nodeString[0] = "/"; sc.nextLine(); for (int i = 0; i < n; i++) { commandLine = sc.nextLine(); command = commandLine.split(" ")[0]; if (commandLine.split(" ").length > 1) { directoryList = commandLine.split(" ")[1]; } if (command.equals("pwd")) { // 현재 디렉토리 찍기 dfs(0); for (int j = 0 ; j < pathDirectory.length; j++) { if (pathDirectory[j] == null) { break; }else { System.out.print(pathDirectory[j]); if (j > 0) System.out.print("/"); } } System.out.println(); flag = false; Arrays.fill(visited,false); Arrays.fill(pathDirectory, null); }else { // cd로 이동하기 // 절대 위치 이동인지 아닌지를 구분 directorys = directoryList.split("/"); if (directoryList.charAt(0) == '/' && directorys.length > 0) { directorys[0] = "/"; currentNode = 0; } for(int j = 0; j < directorys.length; j++) { if (directorys[j].equals("..")) { for (int k = 0; k < nodeTable.length; k++) { if (nodeTable[k][currentNode]) { currentNode = k; break; } } }else { int searchedNode = searchNode(directorys[j]); if (searchedNode == -1){ // 없으면 노드 생성 후 이동 createdNode = insertNode(directorys[j]); nodeTable[currentNode][createdNode] = true; currentNode = createdNode; }else { // currentNode중에 연결된 것이 있으면 그 노드로 이동 currentNode = searchedNode; } } } } } } static int searchNode(String node){ for (int i = 0 ; i < nodeString.length; i++) { if (nodeString[i] == null) { return -1; } if (nodeString[i].equals(node) && nodeTable[currentNode][i]) { return i; } } return -1; } static int insertNode(String node) { for (int i = 0 ; i < nodeString.length; i++) { if (nodeString[i] == null) { nodeString[i] = node; return i; } } return -1; } static void dfs(int v) { visited[v] = true; if (!flag) { pathDirectory[pathNode] = nodeString[v]; } pathNode++; if (v == currentNode) { flag = true; for (int i = pathNode; i < pathDirectory.length; i++) { pathDirectory[i] = null; } } for (int i = 0; i < nodeTable.length; i++) { if (nodeTable[v][i] && !(visited[i]) && !flag) { dfs(i); } } pathNode--; } } |