Header Ads Widget

Alice's library | Hackerearth data structure practice question solution

 Problem:- (Alice's library  | Hackerearth data structure  practice question solution)

Alice is rearranging her library. She takes the innermost shelf and reverses the order of books. She breaks the walls of the shelf. In the end, there will be only books and no shelf walls. Print the order of books.

Opening and closing walls of shelves are shown by '/' and '\' respectively whereas books are represented by lower case alphabets.

Input format

The first line contains string s displaying her library.

Output format

Print only one string displaying Alice's library after rearrangement.

Constraints

2|s|103

Note

The first character of the string is '/' and the last character of the string is '\' indicating outermost walls of the shelf. 

Sample Input
/u/love\i\
Sample Output
iloveu
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

/u/love\i\

Here Katrina breaks the inner most shelf and reverse the order. So the library will be  /uevoli\ . 

Now she breaks the outermost wall and reverses the order. So the library will be iloveu.



Code:-   for (Alice's library  | Hackerearth data structure  practice question solution):-

Here I am going to give you two solutions in first solution I use stack and reverse function which I declare in the code but in the second solution I use the in built stack and reverse function which is define in the STL (Standard template library).

You can submit this solution in c++, c++14 and c++17. this solution is accepted in all this language . and this program I used 
ios_base:: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

this is only for decrease the time to execute the code you can submit the solution without using this 

Solution 1:-

#include<bits/stdc++.h>
using namespace std;

int st[1000];
int top=-1;

// function for push the element
// into stack
int pop()
{
int l=st[top];
top--;
return l;
}

//function for pop
// element from stack
void push(int n)
{
top++;
st[top]=n;
}

//Driver program
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    string str;
    cin>>str;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='/')
         push(i);

        if(str[i]=='\\')
        {
            int l=pop();
            char temp;
            int m=i-1;

         // this is for reverse
            // the string
            for(int j=l+1;j<=(i+l)/2;j++)
            {
             temp=str[j];
             str[j]=str[m];
             str[m]=temp;
             m--;
            
            }
            string ans="";
            // copying str into ans
            for(int j=0;str[j];j++)
            {
             if(j!=l && j!=i)
             ans+=str[j];
            }
            str=ans;
i=i-2;
        }

    }
    cout<<str;
    return 0;
}
 
I think this is complicated for you . this may cause some problem in understanding .So for this I going to give you one more solution in which I used some inbuilt functions. like reverse() , stack<> , push(), pop() , top()  . these all are declared in the STL(standard template library) . And after using this we can understand the code more easily but for understanding we have some pre knowledge  of these function .

Solution 2:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    string str;
    std::stack<int> st ;
    cin>>str;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='/')
         st.push(i);
        if(str[i]=='\\')
        {
            int l=st.top();
            st.pop();
            char temp;
            int m=i-1;
            reverse(str.begin()+l+1,str.begin()+i);
            string ans="";
            for(int j=0;str[j];j++)
            {
             if(j!=l && j!=i)
             ans+=str[j];
            }
            str=ans;
i=i-2;
        }

    }
    cout<<str;
    return 0;
}

Alice's library  | Hackerearth data structure  practice question solution

see more:-


,alice texas public library,
,alice public library,
,alice public library alice texas,
,alice baker library hours,
,alice library ohio university,
,alice baker library eagle wi,
,ohio state university libraries,
,ohio state library database,

Post a Comment

0 Comments