Reverse polish notation problem

question–>http://www.codechef.com/problems/ONP
My solution is working fine on ideone as well on my system and giving right answer for test
given.But it is giving wrong answer on submission. I m not able to find the problem in my
my solution.Plz help.

 #include<iostream> 
 #include<stack>
 #include<stdio.h>
 #include<string.h>
 using namespace std;
 int main()
{ int t;
 cin>>t;
 stack<char> S;
 while(t--)
{
char s1[401],s2[401];
cin>>s1;
int len=strlen(s1);
s1[len]=')';
s1[len+1]='\0';
S.push('(');
int i=0,j=0;
while(!S.empty())
{  
     if(s1[i]=='('||s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/'||s1[i]=='^')
    {
        S.push(s1[i]);
        i++;
    }
    else if(s1[i]==')')
    {
        while(S.top()!='(')
        {
            s2[j]=S.top();
            S.pop();
            j++;
        }
        S.pop();
        i++;
    }
    else
    {
        s2[j]=s1[i];
        i++;j++;
    }

    }

    int len2=strlen(s2);
    s2[len2]='\0';
    cout<<s2;
    cout<<"\n";
    }
    return 0; 
    }

your code is showing weird results . It is showing wrong answer here : https://ideone.com/wrEx0E
and sometime it does not print anything : https://ideone.com/l7ehgg . May be it is due to C++11 . Sorry I`m not able to figure exactly out what is wrong with your code .

Hope this helps .

1 Like