673 - Parentheses Balance

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

Moderator: Board moderators

poka
New poster
Posts: 2
Joined: Wed Dec 17, 2008 2:29 pm

Re: 673 - Parentheses Balance

Post by poka »

Thanks, my program was accepted, just one system.out.println() was wrong....

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 673 - Parentheses Balance

Post by abid_iut »

please help
I am getting WA but why
it passed all the input in the boards
here is the code:

Code: Select all

Removed

pls help :cry:
Last edited by abid_iut on Thu Dec 25, 2008 7:39 am, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 673 - Parentheses Balance

Post by Obaida »

See my PM. You will get a good solution. :)
try_try_try_try_&&&_try@try.com
This may be the address of success.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 673 - Parentheses Balance

Post by abid_iut »

Thanks for ur PM
I am trying in that way. BUT CAN U PLS CHECK MY GIVEN CODE AND TELL ME IS THERE ANY MISTAKE?
and about ur logic in pm
can u pls explain input according to ur PM :

([)]

it should be no.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 673 - Parentheses Balance

Post by Obaida »

This is a special case,
([)]

here when you get any input "[" then you make a Boolean variable 1 and then if you get any ")" then break and no. But if you get "]" before ")" then make the Boolean variable to 0.

Is it clear??? :wink:
try_try_try_try_&&&_try@try.com
This may be the address of success.

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 673 - Parentheses Balance

Post by abid_iut »

Now what is the problem?
It is giving WA but i think It passes all input
here is the code:

Code: Select all

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

char fb[200],tb[200];

int main()
{
	char line[200]="";
	int i,j,n,k,flag,l,p,q,an;
	scanf("%d",&n);
	for(int it=0;it<n;it++){
		gets(line);
		if(line[0]==NULL){printf("Yes\n");continue;}
		k=0;l=0;flag=0;an=0;
		for(i=0;line[i];i++){
			if(line[i]==')' && fb[0]==NULL){flag=1;break;}
			if(line[i]==']' && tb[0]==NULL){flag=1;break;}
			if(line[i]=='(' && line[i+1]==']'){flag=1;break;}
			if(line[i]=='[' && line[i+1]==')'){flag=1;break;}
			if(line[i]=='('){
				fb[k]='(';k++;
				p=k;
			}
			if(line[i]=='['){
				tb[l]='[';l++;
				q=l;
			}
			if(line[i]==')'){
				fb[p-1]=NULL;
				p--;
				k=p;
			}
			if(line[i]==']'){
				tb[q-1]=NULL;
				q--;
				l=q;
			}
		}
		if(flag==1){printf("No\n");}
		else {
			for(i=0;line[i];i++){
				if(fb[i]==NULL && tb[i]==NULL)an=1;
				else {
					an=0;break;
				}
			}
			if(an==1)printf("Yes\n");
			else printf("No\n");
			for(i=0;line[i];i++)line[i]=NULL;
		}
	}
	return 0;
}
pls help :(
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 673 - Parentheses Balance

Post by mf »

Try this input, (which btw I've already posted here just a few days ago):

Code: Select all

1
(([())])
Output is, of course, "No".

Also, it's bad to compare NULL with char.
NULL is a pointer, you shouldn't compare pointers and integers without cast (and usually it's a bad idea to cast ints to pointers, unless you're writing system-level code)

dipal
New poster
Posts: 4
Joined: Mon Apr 27, 2009 9:23 am
Location: BUET

673 - Parentheses Balance

Post by dipal »

hi,
in the Problem description it said that : -
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

but i got wa when i assumed that each of next n lines have string of parenthese [*() || []*].
but got acc when assumed that there can be empty lines and for it Yes should print.

Please somebody make it clear about the tricks in the problem said.

thanks.

diegopedro
New poster
Posts: 1
Joined: Tue Mar 31, 2009 9:12 pm

Re: 673 - Parentheses Balance

Post by diegopedro »

help me, I don't understand my i get wrong answer, my code pass in all tests that i done.
This is my code

#include <iostream>

#include <vector>

#include <string>

#include <stack>



using namespace std;



string confere(string exp){

stack<char>parent;

if (exp.empty() == true) return "Yes";

parent.push(exp[0]);

for(unsigned int j = 1 ; j < exp.size() ; j++){

if(parent.empty() == false){

if(exp[j] == ']'){

if(parent.top()== '[')

parent.pop();

else return "No";

}

else if(exp[j] == ')'){

if(parent.top() == '(')

parent.pop();

else return "No";



}

else parent.push(exp[j]);

}

else parent.push(exp[j]);



}

if (parent.empty() == true)

return "Yes";

else return "No";

}



int main(){

int k;
vector<string>results;

cin>>k;

for(int i=0; i < k ; i++){

string expression;

cin>>expression;

cout<< confere(expression)<<endl;

}



return 0;

}

Xerxes
New poster
Posts: 2
Joined: Sat Sep 12, 2009 8:27 am
Location: Earth

Re: 673 - Parentheses Balance

Post by Xerxes »

I dont think cin can't handle the input data set.I also tried several time with cin like u. bt I failed to manage AC. plz try with getchar() / gets() /getline( , ) .... i think ur idea is ok. I hv used da same logic with one of da mentioned i/o nd managed AC .

Skt
New poster
Posts: 5
Joined: Wed Oct 29, 2008 2:55 pm

Re: 673 - Parentheses Balance

Post by Skt »

removed after AC
Last edited by Skt on Wed Sep 23, 2009 10:20 am, edited 1 time in total.

Skt
New poster
Posts: 5
Joined: Wed Oct 29, 2008 2:55 pm

Re: 673 - Parentheses Balance

Post by Skt »

i actually forgot to clear the stack
while(!s.empty()) s.pop();
this is it

Xerxes
New poster
Posts: 2
Joined: Sat Sep 12, 2009 8:27 am
Location: Earth

Re: 673 - Parentheses Balance

Post by Xerxes »

please don't forget to remove code after finding bug/after AC . :D

kcode
New poster
Posts: 8
Joined: Sun Mar 22, 2009 6:24 am
Location: Aryadesa

Re: 673 - Parentheses Balance

Post by kcode »

I'm getting WA for this. It worked for all tests I fired at it.

EDIT: Code removed after AC
Last edited by kcode on Sun Oct 04, 2009 10:12 am, edited 1 time in total.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 673 - Parentheses Balance

Post by mf »

Try "((" or "))".

And your code is too complex for such an easy task, too many if's. Keep it simpler :)

Post Reply

Return to “Volume 6 (600-699)”