Advertisement
raghuvanshraj

71.cpp

Jul 28th, 2021
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  18.06.2020 06:25:57 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> split_string(string s, char c = ' ') {
  10.     vector<string> ans;
  11.     int start_idx = 0;
  12.     int n = s.size();
  13.     for (int i = 0; i <= n - 1; ++i) {
  14.         if (s[i] == c) {
  15.             ans.push_back(s.substr(start_idx, i - start_idx));
  16.             start_idx = i + 1;
  17.         }
  18.     }
  19.  
  20.     if (start_idx < n) {
  21.         ans.push_back(s.substr(start_idx));
  22.     }
  23.  
  24.     return ans;
  25. }
  26.  
  27. string simplifyPath(string s) {
  28.     vector<string> split = split_string(s, '/');
  29.     stack<string> st;
  30.     for (string x : split) {
  31.         if (not x.empty()) {
  32.             if (x == "..") {
  33.                 if (not st.empty()) {
  34.                     st.pop();
  35.                 }
  36.             } else if (x != ".") {
  37.                 st.push(x);
  38.             }
  39.         }
  40.     }
  41.  
  42.     vector<string> ans;
  43.     while (not st.empty()) {
  44.         ans.push_back(st.top());
  45.         st.pop();
  46.     }
  47.  
  48.     reverse(ans.begin(), ans.end());
  49.     int sz = ans.size();
  50.     string ans_s = "/";
  51.     for (int i = 0; i <= sz - 1; ++i) {
  52.         ans_s = ans_s + ans[i];
  53.         if (i < sz - 1) {
  54.             ans_s = ans_s + "/";
  55.         }
  56.     }
  57.  
  58.     return ans_s;
  59. }
  60.  
  61. int main(int argc, char const *argv[]) {
  62.     string s;
  63.     cin >> s;
  64.  
  65.     cout << simplifyPath(s);
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement