-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyPath.cpp
More file actions
54 lines (46 loc) · 958 Bytes
/
Copy pathSimplifyPath.cpp
File metadata and controls
54 lines (46 loc) · 958 Bytes
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
#include <iostream>
#include <stack>
#include <vector>
#include <cstring>
using namespace std;
string simplifyPath(string path){
vector<string> dir;
string name = "";
for (int i=0;i<path.length();i++){
if (path[i] == '/'){
if (name!=""&&name!=".."&&name!="."){
dir.push_back(name);
name = "";
}
else if (name==".."){
if (dir.size()!=0)
dir.pop_back();
name = "";
}
else if (name=="."){
name = "";
}
}
else{
name = name + path[i];
}
}
if (name != "" && name != "." && name != "..")
dir.push_back(name);
else if (name == ".." && dir.size()>0)
dir.pop_back();
if (dir.size()==0){
return "/";
}
string re = "";
for (int i=0;i<dir.size();i++){
re = re + '/' + dir[i];
}
return re;
}
int main(){
string path = "/.";
string pathnew = simplifyPath(path);
cout<<pathnew;
return 0;
}