path = input().replace("//", '/').split('/') stack = [] for directory in path: if not directory: continue if directory == '..': if stack: stack.pop() else: continue elif directory == '.': continue else: stack.append(directory) print('/' + '/'.join(stack))
使用stringstream分割字符串,将结果存到栈中,如果当前字符串为"."或者空, 不进行任何操作,如果当前字符串为".."则栈中弹出一个元素,否则进栈。 #include <iostream> #include <sstream> #include <algorithm> #include <vector> #include <cmath> #include <climits> #include <string> #include <stack> using namespace std; int main() { string input; cin >> input; stringstream ss(input); stack<string> st; string tmp; while (getline(ss, tmp, '/')) { if (tmp == "." || tmp == "") { continue; } else if (tmp == "..") { if (!st.empty()) st.pop(); } else { st.push(tmp); } } string res = ""; if (st.empty()) { cout << "/" << endl; return 0; } while (!st.empty()) { res = st.top() + res; res = "/" + res; st.pop(); } cout << res << endl; return 0; }
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
String[] strs = s.split("/");
Stack<String> stack = new Stack<String>();
for (int i = 0; i < strs.length; i++) {
String cur = strs[i];
if (cur.equals(".") || cur.equals("")) {
continue;
} else if (cur.equals("..")) {
if(!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(cur);
}
}
String res = "";
while (!stack.isEmpty()) {
String cur = stack.pop();
res = "/" + cur + res;
}
if(res.equals("")) {
res = "/";
}
System.out.println(res);
}
sc.close();
}
}
#include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { int i, j, len; string input, str; vector<string> dir; vector<string> :: iterator it; cin >> input; len = input.length(); for(i = 0; i < len; i++) //遍历字符串,找到所有/之间的子串 { if(input[i] == '/') { j = i + 1; while(j < len && input[j] != '/') { j++; } if(i + 1 < len) { str = input.substr(i + 1, j - i - 1);//取/之间的子串 if(str == "..") { if(!dir.empty()) //出栈前判断栈不空,否则会段错误。 { dir.pop_back(); } } else if(str == ".") { ; } else if(!str.empty()) //入栈前判断子串不是空的 { dir.push_back(str); } } i = j - 1; } } if(dir.empty()) //如果结果栈是空的,也要输出一个 / { cout << "/"; } else { for(it = dir.begin(); it != dir.end(); it++) { cout << "/" << (*it); } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string s; vector<string> r; cin>>s; int l = s.length(); for(int i=0;i<l;i++){ if(s[i] == '/'){ int j = i+1; while(j<l && s[j]!='/') j++; j--; if(i+1 < l){ string t = s.substr(i+1, j-i); if(t == ".."){ if(!r.empty()) r.pop_back(); }else if(t != "." && t!="") r.push_back(t); } i = j; } } if(r.empty()) printf("/"); else for(auto &t: r) printf("/%s", t.c_str()); return 0; }
import os print(os.path.normpath(input()))今天笔试遇到了这题,用栈A了半天没考虑../的情况,还不如直接不讲武德用os
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); LinkedList<String> stack = new LinkedList<>(); for (String s : br.readLine().split("/")) { if (s.equals("..") && !stack.isEmpty()) stack.removeLast(); else if (!s.equals("") && !s.equals(".") && !s.equals("..")) stack.addLast(s); } System.out.println("/" + String.join("/", new ArrayList<>(stack))); } }
import re ss=raw_input().strip('\n') arr=re.split('/+',ss) tp1=[] for i in arr: if i != '.': tp1.append(i) tp2=[] for j in tp1: if j !='..': tp2.append(j) else: if len(tp2)==1: continue else: tp2.pop() res='/'.join(tp2) if len(res)==1: print res else: print res.rstrip('/')
#include <iostream> #include <string> #include <stack> #include <deque> using namespace std; int main(){ string input; while(cin>>input){ stack<char> temp; for(int i = 0;i < input.size();i++){ if((i+1 < input.size())&&(input[i]=='/'&&input[i+1]=='/')){ continue; } else if((i+2 < input.size())&&((input[i]=='/'&&input[i+1]=='.'&&input[i+2]=='/'))) { i += 1; continue; } else if((i+3 < input.size())&&(input[i]=='/'&&input[i+1]=='.'&&input[i+2]=='.'&&input[i+3]=='/')){ for(;temp.size() > 0;){ if(temp.top()!='/'){ temp.pop(); } else break; } if(temp.size() > 0) temp.pop(); i += 2; }// \..情况 else temp.push(input[i]); } deque<char> temp0; int j = temp.size(); for(int i = 0;i < j;i++){ temp0.push_front(temp.top()); temp.pop(); } string res(temp0.begin(),temp0.end()); j = res.size(); for(;res.size()>0;) if(res[res.size()-1] == '/') res.erase(res.end()-1); else break; if(res.size()==0) res.push_back('/'); cout<<res<<endl; input.clear(); } return 0; }