Page 5 of 11

Posted: Sun Jul 23, 2006 5:59 pm
by IRA
I also got WA in this problem.

Posted: Sat Jul 29, 2006 10:29 am
by Kallol
I have absolutely bo idea why I am getting WA !!!

I have read all the posts here and my program passed all the input cases. But I am gettin WA again and again !!
Here i have no option but to seek ur help with my code. Why I am getting wrong?? Do you have any critical input that yields wrong output by my code. Plz help !!

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>

char s[50000];

int next(int i)
{
	while(s[i]!=')')
	{
		i++;
	}

	i--;
	return i;
}

char convert(char c)
{
	if(c=='+')
	{
		return 'p';
	}
	else if(c=='-')
	{
		return 's';
	}
	else if(c=='*')
	{
		return 'm';
	}
	else if(c=='/')
	{
		return 'd';
	}
	else if(c=='p')
	{
		return '+';
	}
	else if(c=='s')
	{
		return '-';
	}
	else if(c=='m')
	{
		return '*';
	}
	else if(c=='d')
	{
		return '/';
	}
	else
	{
		return 0;
	}
}
void push(int pos,int c)
{
	int i=strlen(s)+1;
	while(i>pos)
	{
		s[i]=s[i-1];
		i--;
	}
	s[pos]=c;
	return;
}
void find_back_n_push(int pos,int c)
{
	int i=pos-1,sum=0;
	while(i)
	{
		if(s[i]==')')
		{
			sum++;
		}
		else if(s[i]=='(')
		{
			sum--;
		}
		if(sum==0)
		{
			break;
		}
		i--;
	}
	push(i,'(');
	
	return;
}
void find_front_n_push(int pos,int c)
{
	int i=pos+1,sum=0;
	while(s[i])
	{
		if(s[i]==')')
		{
			sum++;
		}
		else if(s[i]=='(')
		{
			sum--;
		}
		if(sum==0)
		{
			break;
		}
		i++;
	}
	push(i+1,convert(c));
	push(i+2,')');
	
	
	return;
}
int Evaluate(int b,int l)
{
	int i,r=0,rsum=0;
	for(i=b;i<=l;i+=(r+1))
	{
		r=0;
		if(s[i]=='(')
		{
			r=Evaluate(i+1,next(i));
		}
		rsum+=r;
		l+=r;
	}
	r=0;
	for(i=b;i<=l;)
	{
		if(s[i]=='*' || s[i]=='/')
		{
			char c=s[i];
			find_back_n_push(i,c);

			find_front_n_push(i+1,c);
			i+=1;
			s[i]=' ';
			r+=3;
			rsum+=3;
			l+=3;
		}
		i++;
	}
	for(i=b;i<=l;)
	{
		if(s[i]=='+' || s[i]=='-')
		{
			char c=s[i];
			find_back_n_push(i,c);
			find_front_n_push(i+1,c);
			i+=1;
			s[i]=' ';
			r+=3;
			rsum+=3;
			l+=3;		
		}
		i++;
	}
	return rsum;
}
void print()
{
	int i=0;
	while(s[i])
	{
		if(s[i]>='0' && s[i]<='9')
		{
			printf("%c",s[i]);
		}
		if(isalpha(s[i]))
		{
			printf("%c",convert(s[i]));
		}
		i++;
	}
	printf("\n");
	return;
}
int main(void)
{
	char temp[10];
	int test;
	scanf("%d",&test);
	gets(temp);
	gets(temp);
	
	while(test--)
	{
		strcpy(s,"");
		while(1)
		{
			if(!gets(temp))
			{
				break;
			}
			if(strcmp(temp,"")==0)
			{
				break;
			}
			strcat(s,temp);
		}
		int l=strlen(s);
	//	s[l]='\0';
		int vua=Evaluate(0,l-1);
		print();
		if(test)
		{
			printf("\n");
		}
	
	}
	return 0;
}
Waiting for help ...

727 - TLE

Posted: Mon Jul 31, 2006 1:15 am
by unaben
I am getting TLE in this problem. :( Could someone plz take at look at my code. Here it is:

//CODE REMOVED AFTER AC

Thanx in advance :wink:

Posted: Mon Jul 31, 2006 1:27 am
by unaben
I got AC. :D

Silly mistake :oops:

Posted: Mon Jul 31, 2006 2:03 am
by sml
Delete yer code. ;) :D

And congrats.

Posted: Tue Nov 21, 2006 8:12 pm
by Donotalo
i've tried all the input/output set in the forum and all seem work well. still getting WA. can anyone kindly check my i/o set? thanks in advance.

INPUT

Code: Select all

11

3
+
4
*
2

3
*
4
+
2

(
3
+
4
)
*
2

3
*
(
4
+
2
)

(
3
+
4
)
*
(
4
+
5
)

3
+
4
-
5

(
3
+
2
)
*
5

(
5
+
(
5
+
5
)
)

(
1
)

(
1
/
2
*
3
+
4
)
5

(
(
2
)
)
OUTPUT

Code: Select all

342*+

34*2+

34+2*

342+*

34+45+*

34+5-

32+5*

555++

1

12/3*4+5*

2

Posted: Wed Nov 22, 2006 7:21 am
by Jan
My accepted code returns the same output. Try the following test case

Input:

Code: Select all

1

(
3
+
(
3
+
2
)
*
(
4
+
5
+
2
)
*
(
1
+
2
-
3
/
(
1
-
2
*
(
4
+
4
)
)
)
)
-
3
*
5
Output:

Code: Select all

332+45+2+*12+31244+*-/-*+35*-
Hope it helps.

Posted: Sat Jun 02, 2007 5:47 pm
by abhiramn

Code: Select all

CODE ACCEPTED
I am getting a RTE. Please tell me what the problem could be.

Posted: Sat Jun 02, 2007 7:23 pm
by Jan
I just used same input twice and your code failed. Check the cases and I/O format.

Input:

Code: Select all

2

(
3
+
2
)
*
5

(
3
+
2
)
*
5
Output:

Code: Select all

32+5*

32+5*
Hope these help.

@jan

Posted: Sat Jun 02, 2007 7:55 pm
by abhiramn
I did try those two inputs, but my code gives the same correct result. I did however change the code a little bit. And here it is. Same problem once again, RTE :evil:
Thanks for your reply. Hoping you would help me out again.

Code: Select all

CODE WAS ACCEPTED

Posted: Sat Jun 02, 2007 8:08 pm
by helloneo
I did run your code and I got segmentation fault which is RTE for sample input..
I guess taking input is wrong..

Thank you guys

Posted: Sun Jun 03, 2007 6:49 am
by abhiramn
I got AC. The problem was in this condition.....

if (ch=='\n')
.....break;


I changed it to

if(ch=='\n'||ch==EOF)
.....break;

:D :D :D :D Thanks a lot.

Posted: Fri Jul 13, 2007 10:20 am
by abdullah<cse du>
Hi,

727 gives many times wrong ans to me. I have searched all the topics of 727 in the board and match all the input/output. All the i/o match correctly. Please help me to locate my errors.

Code: Select all


Code has been cut after a.c.
The problem is one the line
gets(input);
I have cut this line and got accepted. Thanks Jan vai for his help.
Thanks all.
ABDULLAH.

Posted: Fri Jul 13, 2007 1:21 pm
by Jan
Try the cases.

Input:

Code: Select all

3

4
+
4
*
2

5
-
1
/
2

6
/
2
Output:

Code: Select all

442*+

512/-

62/
Hope these help.

Posted: Fri Jul 13, 2007 2:06 pm
by abdullah<cse du>
Jan vai,

My program gives the same output. Please help me to locate my errors.

Thanks for reply.

ABDULLAH.