Advertisement
Valkyrie006

Untitled

Oct 8th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. // Alice Library - https://code.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/katrina-and-library-c2ed51f3/
  4. int main() {
  5.   string s;
  6.     cin>>s;
  7.     int n = s.length();
  8.     stack<char> st;
  9.     for(int i=0;i<n;i++)
  10.     {
  11.         if(s[i]=='/' or (s[i]>='a' and s[i]<='z'))
  12.         {
  13.             st.push(s[i]);
  14.         }
  15.         else
  16.         {
  17.             // reverse the string upto '/' in stack
  18.             string temp = "";
  19.             while(st.top()!='/')
  20.             {
  21.                 temp+=st.top();
  22.                 st.pop();
  23.             }
  24.             // remove '/' from stack's top
  25.             st.pop();
  26.             for(int j=0;j<temp.length();j++)
  27.             {
  28.                 st.push(temp[j]);
  29.             }
  30.         }
  31.     }
  32.     string ans = "";
  33.     while(!st.empty())
  34.     {
  35.         ans+=st.top();
  36.         st.pop();
  37.     }
  38.     reverse(ans.begin(), ans.end());
  39.     cout<<ans;
  40.   return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement