727 - Equation

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Sun Jul 23, 2006 5:59 pm

I also got WA in this problem.

User avatar
Kallol
Learning poster
Posts: 100
Joined: Sun Nov 13, 2005 8:56 am

Post by Kallol » Sat Jul 29, 2006 10:29 am

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 ...
Syed Ishtiaque Ahmed Kallol
CSE,BUET
Bangladesh

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm

727 - TLE

Post by unaben » Mon Jul 31, 2006 1:15 am

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:
Last edited by unaben on Mon Jul 31, 2006 5:06 pm, edited 1 time in total.

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm

Post by unaben » Mon Jul 31, 2006 1:27 am

I got AC. :D

Silly mistake :oops:

sml
New poster
Posts: 15
Joined: Mon Jul 24, 2006 3:34 pm

Post by sml » Mon Jul 31, 2006 2:03 am

Delete yer code. ;) :D

And congrats.

User avatar
Donotalo
Learning poster
Posts: 56
Joined: Sat Aug 20, 2005 11:05 am
Location: Bangladesh

Post by Donotalo » Tue Nov 21, 2006 8:12 pm

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
Image

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Nov 22, 2006 7:21 am

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.
Ami ekhono shopno dekhi...
HomePage

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

Post by abhiramn » Sat Jun 02, 2007 5:47 pm

Code: Select all

CODE ACCEPTED
I am getting a RTE. Please tell me what the problem could be.
Last edited by abhiramn on Sun Jun 03, 2007 6:50 am, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sat Jun 02, 2007 7:23 pm

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.
Ami ekhono shopno dekhi...
HomePage

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

@jan

Post by abhiramn » Sat Jun 02, 2007 7:55 pm

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
Last edited by abhiramn on Sun Jun 03, 2007 6:49 am, edited 1 time in total.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo » Sat Jun 02, 2007 8:08 pm

I did run your code and I got segmentation fault which is RTE for sample input..
I guess taking input is wrong..

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

Thank you guys

Post by abhiramn » Sun Jun 03, 2007 6:49 am

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.

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Location: Bangladesh(CSE DU)
Contact:

Post by abdullah<cse du> » Fri Jul 13, 2007 10:20 am

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.
Last edited by abdullah<cse du> on Fri Jul 13, 2007 5:43 pm, edited 1 time in total.
We were in past, we are in past and we will go in past.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Fri Jul 13, 2007 1:21 pm

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.
Ami ekhono shopno dekhi...
HomePage

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Location: Bangladesh(CSE DU)
Contact:

Post by abdullah<cse du> » Fri Jul 13, 2007 2:06 pm

Jan vai,

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

Thanks for reply.

ABDULLAH.
We were in past, we are in past and we will go in past.

Post Reply

Return to “Volume 7 (700-799)”