-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem158C.java
More file actions
85 lines (78 loc) · 1.63 KB
/
Copy pathProblem158C.java
File metadata and controls
85 lines (78 loc) · 1.63 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
85
package CodeForces;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
public class Problem158C
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
int n = Integer.valueOf(scan.nextLine());
String[] instructions = new String[n];
LinkedList<String> directory = new LinkedList<String>();
for (int i = 0; i < n; i++)
{
instructions[i] = scan.nextLine();
}
scan.close();
for (int i = 0; i < n; i++)
{
if (instructions[i].equals("pwd"))
{
output(directory);
}
else if (instructions[i].startsWith("cd"))
{
if (instructions[i].equals("cd .."))
{
if (!directory.isEmpty())
directory.removeLast();
}
else if (instructions[i].startsWith("cd /"))
{
directory.clear();
String substr[];
substr = instructions[i].split("/");
for (int j = 1; j < substr.length; j++)
{
if (substr[j].equals("..") && !directory.isEmpty())
{
directory.removeLast();
}
else
{
directory.add(substr[j]);
}
}
}
else
{
String substr[];
substr = instructions[i].split("/");
substr[0] = substr[0].substring(3);
for (int j = 0; j < substr.length; j++)
{
if (substr[j].equals("..") && !directory.isEmpty())
{
directory.removeLast();
}
else
{
directory.add(substr[j]);
}
}
}
}
}
}
public static void output(LinkedList<String> l)
{
for (String a : l)
{
System.out.print("/");
System.out.print(a);
}
System.out.print("/");
System.out.println("");
}
}