673 - Parentheses Balance
Moderator: Board moderators
Re: 673 - Parentheses Balance
Thanks, my program was accepted, just one system.out.println() was wrong....
Re: 673 - Parentheses Balance
please help
I am getting WA but why
it passed all the input in the boards
here is the code:
pls 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

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/
http://akanoi.webs.com/
Re: 673 - Parentheses Balance
See my PM. You will get a good solution. 

try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.
Re: 673 - Parentheses Balance
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 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/
http://akanoi.webs.com/
Re: 673 - Parentheses Balance
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???
([)]
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???

try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.
Re: 673 - Parentheses Balance
Now what is the problem?
It is giving WA but i think It passes all input
here is the code:
pls help 
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;
}

i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
http://akanoi.webs.com/
Re: 673 - Parentheses Balance
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)
Code: Select all
1
(([())])
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
hi,
in the Problem description it said that : -
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.
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.
-
- New poster
- Posts: 1
- Joined: Tue Mar 31, 2009 9:12 pm
Re: 673 - Parentheses Balance
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;
}
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
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
removed after AC
Last edited by Skt on Wed Sep 23, 2009 10:20 am, edited 1 time in total.
Re: 673 - Parentheses Balance
i actually forgot to clear the stack
while(!s.empty()) s.pop();
this is it
while(!s.empty()) s.pop();
this is it
Re: 673 - Parentheses Balance
please don't forget to remove code after finding bug/after AC . 

Re: 673 - Parentheses Balance
I'm getting WA for this. It worked for all tests I fired at it.
EDIT: Code removed after AC
EDIT: Code removed after AC
Last edited by kcode on Sun Oct 04, 2009 10:12 am, edited 1 time in total.
Re: 673 - Parentheses Balance
Try "((" or "))".
And your code is too complex for such an easy task, too many if's. Keep it simpler :)
And your code is too complex for such an easy task, too many if's. Keep it simpler :)