Page 10 of 17
Re: 673 - Parentheses Balance
Posted: Fri Dec 19, 2008 1:48 pm
by poka
Thanks, my program was accepted, just one system.out.println() was wrong....
Re: 673 - Parentheses Balance
Posted: Tue Dec 23, 2008 7:15 pm
by abid_iut
please help
I am getting WA but why
it passed all the input in the boards
here is the code:
pls help

Re: 673 - Parentheses Balance
Posted: Wed Dec 24, 2008 8:48 am
by Obaida
See my PM. You will get a good solution.

Re: 673 - Parentheses Balance
Posted: Wed Dec 24, 2008 10:11 am
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.
Re: 673 - Parentheses Balance
Posted: Wed Dec 24, 2008 11:10 am
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???

Re: 673 - Parentheses Balance
Posted: Thu Dec 25, 2008 7:41 am
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

Re: 673 - Parentheses Balance
Posted: Thu Dec 25, 2008 8:48 am
by mf
Try this input, (which btw I've already posted here just a few days ago):
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)
673 - Parentheses Balance
Posted: Fri May 01, 2009 6:11 pm
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.
Re: 673 - Parentheses Balance
Posted: Sat Aug 01, 2009 4:54 am
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;
}
Re: 673 - Parentheses Balance
Posted: Tue Sep 15, 2009 6:04 pm
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 .
Re: 673 - Parentheses Balance
Posted: Tue Sep 22, 2009 10:27 pm
by Skt
removed after AC
Re: 673 - Parentheses Balance
Posted: Tue Sep 22, 2009 10:46 pm
by Skt
i actually forgot to clear the stack
while(!s.empty()) s.pop();
this is it
Re: 673 - Parentheses Balance
Posted: Tue Sep 22, 2009 11:43 pm
by Xerxes
please don't forget to remove code after finding bug/after AC .

Re: 673 - Parentheses Balance
Posted: Sat Oct 03, 2009 11:30 pm
by kcode
I'm getting WA for this. It worked for all tests I fired at it.
EDIT: Code removed after AC
Re: 673 - Parentheses Balance
Posted: Sun Oct 04, 2009 12:18 am
by mf
Try "((" or "))".
And your code is too complex for such an easy task, too many if's. Keep it simpler :)