-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0388.cpp
More file actions
35 lines (30 loc) · 978 Bytes
/
Copy path0388.cpp
File metadata and controls
35 lines (30 loc) · 978 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
class Solution {
public:
int lengthLongestPath(const string &input) const {
const int n = size(input);
stack<pair<int, int>> st;
int res = 0;
st.emplace(0, 0);
for (int i = 0, dir_len = 0; i < n;) {
if (input[i] == '\n') {
int cnt = 0;
while (++i < n && input[i] == '\t')
cnt++;
while (cnt < st.top().first)
st.pop();
if (cnt > st.top().first) st.emplace(cnt, st.top().second + dir_len + 1);
} else {
int cnt = 0, is_file = 0;
while (i < n && input[i] != '\n') {
if (input[i] == '.') is_file = true;
i++, cnt++;
}
if (is_file)
res = max(res, st.top().second + cnt);
else
dir_len = cnt;
}
}
return res;
}
};