TRANSFORM THE EXPRESSION

http://www.codechef.com/viewsolution/6585917

PROBLEM-----> ONP Problem - CodeChef

this solution is showing WA in codechef, runtime error in ideone and working perfectly in code::blocks

The test cases which are working fine in code::blocks are showing runtime erroe at ideone.
WHY???

I have commented my algorithm for better understanding

The problem is in your method for fast string input i.e., str_input, modify it for newline charater.

The newline character is the reason for such weird behavior.

t=input(); with str_input(s); is giving you runtime error because during reading the integer t, the new line (‘\n’) is read and skipped. So in next str_input() method while (c!=32 && c!=10), the check for c != 10 is skipping the next (second ) string input.

If the input was


3
(a+(b*c))

((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

then the runtime error would not occur ( see the extra line between first and second string ), see it on ideone here.

See your same code ACcepted here with out using the " str_input " method and rest of the code remains the same.

1 Like

HEY, I AM GETTING WRONG ANSWER CAN ANYONE HELP…

#include
using namespace std;
main()
{
int t,j=0,top=-1;
char a[400],stack[400],b[400];
cin>>t;
while(t–)
{
cin>>a;

	for(int i=0;a[i]!='\0';i++)
  {
        if( (a[i]=='(')||(a[i]=='+')||(a[i]=='-')||(a[i]=='*')||(a[i]=='/')||(a[i]=='^') )
        {
        	top++;
        	stack[top]=a[i];
    	    
        }
        else if(a[i]==')')
        {
    	   b[j]=stack[top];
    	   j++;
    	   top--;
    	   while(stack[top]!='(')
      	    {
      	   	b[j]=stack[top];
      	   j++;
			 }
      	   top--;
    	  
        }
       else
       {
  	       b[j]=a[i];
           j++;
          
       }
    }

   cout<<b<<endl;
   j=0,top=-1;
}

}

hi thnx for pointing it out… i have optimized my str_input() function to check for leading spaces or new lines before string input but what i found the problem was actually with me, not terminating the string “exp” with ‘\0’
which was causing the runtime error.