forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_simulation_n.cpp
More file actions
57 lines (51 loc) · 1.41 KB
/
AC_simulation_n.cpp
File metadata and controls
57 lines (51 loc) · 1.41 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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2015-02-04 09:04:15
* Descripton: simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
string simplifyPath(string path) {
if (!path.empty() && path[path.length() - 1] != '/')
path += '/';
vector<string> folders;
int cur = -1;
for (auto &i : path) {
if (i == '/') {
if (cur >= 0 && folders[cur] == "..") {
folders.pop_back();
--cur;
if (cur >= 0)
folders[cur] = "";
else {
folders.push_back("");
++cur;
}
} else if (cur >= 0 && folders[cur] == ".") {
folders[cur] = "";
} else if (cur < 0 || folders[cur] != "") {
folders.push_back("");
++cur;
}
} else {
folders[cur].push_back(i);
}
}
string res = "";
for (auto &i : folders)
if (!i.empty())
res += "/" + i;
return res == "" ? "/" : res;
}
};
int main() {
string path;
Solution s;
while (cin >> path)
cout << s.simplifyPath(path) << endl;
return 0;
}