【Codeforces 158C】Cd and pwd commands
【链接】 我是链接,点我呀:)
【题意】
【题解】
用一个list表示当前的路径 如果路径以/开头则表示需要清空当前路径重新走路 否则在原来路径的基础上继续加就可以了【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)3000;
static class Task{
ArrayList cwd= new ArrayList();
String getCWD() {
StringBuilder sb = new StringBuilder("/");
for (int i = 0;i < cwd.size();i++) {
String s = (String)cwd.get(i);
sb.append(s+"/");
}
return sb.toString();
}
public void solve(InputReader in,PrintWriter out) {
int n;
n = in.nextInt();
for (int i = 1;i <= n;i++) {
String ope = in.next();
if (ope.equals(new String("pwd"))) {
out.println(getCWD());
}else {
String p = in.next();
String []parameters;
if (p.charAt(0)=='/') {
cwd.clear();
parameters = p.substring(1,p.length()).split("/");
}else {
parameters = p.split("/");
}
for (int j = 0;j < parameters.length;j++) {
if (!parameters[j].equals("..")) {
cwd.add(parameters[j]);
}else {
cwd.remove((int)cwd.size()-1);
}
}
}
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}