1 class Solution {
2 public:
3 string simplifyPath(string path) {
4 deque<string> qs;
5 string result;
6 int plen = path.size();
7 string::size_type curindex = 0, lastindex = 0;
8
9 while (lastindex < plen && (curindex = path.find("/", lastindex)) != string::npos)
10 {
11 if(path.find("//", lastindex))
12 {
13 qs.push_back(path.substr(lastindex, curindex+1-lastindex));
14 lastindex = curindex+2;
15 }else if (path.find("./", lastindex)) {
16 lastindex = curindex+1;
17 }else if (path.find(".//", lastindex)) {
18 lastindex = curindex+2;
19 }else if (path.find("../", lastindex)) {
20 qs.pop_back(); // go back one step
21 lastindex = curindex+1;
22 }else if (path.find("..//", lastindex)) {
23 qs.pop_back();
24 lastindex = curindex+2;
25 }else {
26 qs.push_back(path.substr(lastindex, curindex+1-lastindex));
27 lastindex = curindex+1;
28 }
29 }
30
31 while (!qs.empty()) {
32 string tmp = qs.front();
33 qs.pop_front();
34 result.append(tmp);
35 }
36 if(result.size() != 1){
37 result.resize(result.size()-1);
38 }
39 return result;
40 }
41 };