-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify_Path.cpp
More file actions
90 lines (82 loc) · 1.68 KB
/
Copy pathSimplify_Path.cpp
File metadata and controls
90 lines (82 loc) · 1.68 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* author: sazzad
* created: 10.01.2026 18:09:24
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pa pair<int, int>
#define pa2 pair<ll, ll>
#define pa3 pair<ll, int>
#define nl cout << '\n'
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
void simple_op(stack<string> &st, string &folderName)
{
if (folderName.length() > 0 && folderName != ".")
{
if (folderName == ".." && !st.empty())
st.pop();
else if (folderName != "..")
st.push(folderName);
}
folderName = "";
}
void simplify_path(stack<string> &st, string path)
{
string folderName = "";
for (int i = 0; i < path.length(); i++)
{
if (path[i] != '/')
folderName += path[i];
else
simple_op(st, folderName);
}
simple_op(st, folderName);
}
void reverse_stack(stack<string> &st)
{
if (st.empty())
return;
string s = st.top();
st.pop();
reverse_stack(st);
stack<string> cp;
while (!st.empty())
{
cp.push(st.top());
st.pop();
}
st.push(s);
while (!cp.empty())
{
st.push(cp.top());
cp.pop();
}
}
class Solution
{
public:
string simplifyPath(string path)
{
stack<string> st;
string s = "";
simplify_path(st, path);
if (st.empty())
return "/";
reverse_stack(st);
while (!st.empty())
{
s += "/" + st.top();
st.pop();
}
return s;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solution s = Solution();
cout << s.simplifyPath("/.../a/../b/c/../d/./") << '\n';
}