-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path71.cpp
More file actions
executable file
·64 lines (63 loc) · 1.65 KB
/
71.cpp
File metadata and controls
executable file
·64 lines (63 loc) · 1.65 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
#include <iostream>
#include <stdio.h>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
string simplifyPath(string path) {
stack<string> st;
int tmp = -1;
string dic = "";
for (int i = 0; i < path.size();i++){
if (tmp == -1 && path[i] == '/'){
dic="";
tmp = i;
}else if (path[i] == '/'){
if (dic =="."){
tmp = i;
dic="";
continue;
}
if (dic == ".."){
if (!(st.empty()))
st.pop();
}else{
if (dic != "")
st.push(dic);
}
tmp = i;
dic="";
}else{
dic+=path[i];
if (i == path.size()-1){
if (dic == ".."){
if (!st.empty())
st.pop();
}else if (dic != "." && dic !=""){
st.push(dic);
}
}
}
}
string answer = "";
while (!st.empty()){
string now = st.top();
if (now == ""){
st.pop();
continue;
}
if (answer == "")
answer = now + answer;
else
answer = now + "/" +answer;
st.pop();
}
answer = "/"+answer;
return answer;
}
};
int main(){
Solution s;
cout<<s.simplifyPath("/a//b////c/d//././/..")<<endl;
}