×

# My solution for Transform the expression gives Wrong answer.

 0 Problem Link - http://www.codechef.com/problems/ONP My program runs for all inputs I tried, but still gives wrong answer. #include #include #include #include using namespace std; int transform(char a[401]) { int n = strlen(a),i,j,k,l,m,f; for(int o=1;;o++) { f=0; for(i=n-1;i>=0;i--) { if(a[i]=='(') { a[i]=-1; f=1; break; } } if (f==0) { break; } for(j=i+1;;j++) { if(isalpha(a[j])==0&&a[j]!=-1) break; } for(k=j;;k++) { if(isalpha(a[k])!=0) { break; } } while((a[k]==-1)||(isalpha(a[k])!=0)||(a[k]=='(')||(a[k]==')')) { k--; } for(l=k;;l++) { if(a[l]==')') { a[l]=a[k]; a[k]=-1; break; } } } for(m=0;m>times; for(q=1;q<=times;q++) { gets(a[q]); transform(a[q]); } return 0; }  asked 29 Jul '14, 19:40 6★c1_6 81●1●10 accept rate: 0%

 1 you are getting wrong answers for these type of test cases ((e+((a+b)+(c+d)))+(f+((e+d)+(g+h)))) correct answer for above test case is eab+cd+++fed+gh++++ but your answer is eabcd++++fed+gh++++ try to debug this test case you may get correct answer and don't forget to print new line that is '\n' after every test case. i hope this test case will be helpful to you. and a small hint for this problem from my side is traverse the string when ever you encounter any operator push into stack else if you encounter closed bracket pop the operator present in the stack else other than brackets or operator that is any character just print that character if you want to look at the code with the above algorithm you can look at this Thank you, Happy coding :) answered 29 Jul '14, 21:08 2★pudge 758●4●13 accept rate: 37% Thanks... I guess I need to learn more before trying out such problems... I have no idea what a stack is. (29 Jul '14, 22:14) c1_66★ it's ok you can just google it what stack is.... :) (29 Jul '14, 22:33) pudge2★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,914
×130
×17
×13

question asked: 29 Jul '14, 19:40

question was seen: 1,181 times

last updated: 29 Jul '14, 22:33