You are not logged in. Please login at www.codechef.com to post your questions!

×

TRANSFORM THE EXPRESSION

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

PROBLEM-----> http://www.codechef.com/problems/ONP/

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

asked 27 Mar '15, 22:58

rishu97kr's gravatar image

2★rishu97kr
2528
accept rate: 0%


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.

link

answered 28 Mar '15, 13:44

mediocoder's gravatar image

3★mediocoder
1.1k313
accept rate: 36%

edited 28 Mar '15, 14:01

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.

(28 Mar '15, 21:51) rishu97kr2★

HEY, I AM GETTING WRONG ANSWER CAN ANYONE HELP..

include<iostream>

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;
}

}

link

answered 28 Feb '16, 16:01

nanhe's gravatar image

3★nanhe
413
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,911
×46
×13

question asked: 27 Mar '15, 22:58

question was seen: 1,419 times

last updated: 28 Feb '16, 16:01