Transform the Expression editorial SPOJ - UCS - Unleash-Coding-Skills

Thursday, 3 May 2018

Transform the Expression editorial SPOJ - UCS

The link for the problem  Transform the Expression:

http://www.spoj.com/problems/ONP/

The logic for the problem is quite simple print in output if the given input character is in 'a' to 'z'.In case if you encounter any symbol push into stack and pop from the stack when you encounter ')' and print it.

The CPP solution for the problem  Transform the Expression:


#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main() {
int t;
cin>>t;
int i,j,k,io,uo;
for(int ii=0;ii<t;ii++)
{
stack<char>st;
string s1,res;
char cc;
cin>>s1;
for(i=0;i<s1.length();i++)
{
if(s1[i]>='a'&&s1[i]<='z')
{
cout<<s1[i];       
}
else if(s1[i]==')')
{
cout<<st.top();
st.pop();
}
else if(s1[i]!='(')
{
st.push(s1[i]);
}
}
cout<<endl;
}
return 0;
}

Happy Coding................

No comments:

Post a comment