673 - Parentheses Balance
Moderator: Board moderators
-
- Learning poster
- Posts: 57
- Joined: Fri Oct 10, 2003 11:01 pm
- Location: in front of PC
- Contact:
help WAAAAAAA
hello all.
i get WA...
here is my code
i amm used recursive.
thx in advance...
i get WA...
here is my code
Code: Select all
#include <stdio.h>
char flag, formed, line[130];
int count;
void read(char prev, char *t, char X){
if( flag )return ;
char s, x = true;
s = X? X: line[count++];
if( (s=='\r' || s == -1) && prev == -1 ){formed = 1; return;}
if( s == ')' && prev !='(' || s==']' && prev!='[' ){
flag = true;
return ;
}
if( s == ')' || s==']' ){
*t = false;
return;
}
if(s=='\r'){
formed = flag = true;
return;
}
read(s, &x, 0);
if(x && flag==0){
s = line[count++];
/* it must be closing sequence */
if(s=='\r'){
formed = flag = true;
return;
}
while( s == '(' || s == '[' ){
read(prev,&x, s);
s = line[count++];
}
}
}
void main(void){
//freopen("i:\\a\\673.txt","rb",stdin);
int cas; char t;
scanf("%d", &cas); t = getchar(); t = getchar();
while( cas--) {
gets( line );
if( line[0] == 13 ){
puts("Yes");
}
else{
count = 0;
flag = formed = false; t = -1;
while( !formed && !flag ){
read(-1, &t, 0);
}
flag?puts("No"):puts("Yes");
}
}
}
thx in advance...
There are two tragedies in life one is to lose your hearts' desire and another is to gain it --- GBS.
Re: help WAAAAAAA
I didn't look through your recursive body, but this might be the problem of your code. A newline in Un*x is represented only by a character '\n', but in Windows, it's represented by '\r' '\n'. Thus when your program runs under Un*x, the first getchar() reads the newline, the second getchar() reads the first character of the first input, therefore your code gives the wrong output. You can simply use gets(line) again to get rid of the newline.I LIKE GN wrote:Code: Select all
scanf("%d", &cas); t = getchar(); t = getchar();
Hope this will make your code AC

LPH [acronym]
= Let Program Heal us
-- New Uncyclopedian Dictionary, Minmei Publishing Co.
= Let Program Heal us
-- New Uncyclopedian Dictionary, Minmei Publishing Co.
-
- Learning poster
- Posts: 57
- Joined: Fri Oct 10, 2003 11:01 pm
- Location: in front of PC
- Contact:
sorry 4 late reply...
the code was modified...
still wa
please help...
the code was modified...
still wa
Code: Select all
#include <stdio.h>
char flag, formed, line[130];
int count;
void read(char prev, char *t, char X){
if( flag )return ;
char s, x = true;
s = X? X: line[count++];
if( (s=='\r' || s=='\n' || s == -1) && prev == -1 ){formed = 1; return;}
if( s == ')' && prev !='(' || s==']' && prev!='[' ){
flag = true;
return ;
}
if( s == ')' || s==']' ){
*t = false;
return;
}
if(s=='\r' || s=='\n'){
formed = flag = true;
return;
}
read(s, &x, 0);
if(x && flag==0){
s = line[count++];
/* it must be closing sequence */
if(s=='\r' || s=='\n'){
formed = flag = true;
return;
}
while( s == '(' || s == '[' ){
read(prev,&x, s);
s = line[count++];
}
}
}
void main(void){
//freopen("i:\\a\\673.txt","rb",stdin);
int cas; char t;
scanf("%d", &cas); t = getchar();
while( cas--) {
gets( line );
if( line[0] == '\n' || line[0] == 13 ){
puts("Yes");
}
else{
count = 0;
flag = formed = false; t = -1;
while( !formed && !flag ){
read(-1, &t, 0);
}
flag?puts("No"):puts("Yes");
}
}
}
There are two tragedies in life one is to lose your hearts' desire and another is to gain it --- GBS.
673 why wa
this is my code.i dont understand why wa.plz help me.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char t[200],t1[200],t2[200],t3[200],t4[200];
char t5[200];
int i,j,k,m=0,n=0,l,check=0,check1=0,k1=0,k2=0,m1=0,m2=0,m3=0,m4=0;
char s[200];
int k3,k4;
long p;
p=atol(gets(s));
for(l=0;l<p;l++)
{
gets(t);
i=strlen(t);
if(i==0)
{
printf("Yes\n");
continue;
}
for(j=0;j<i;j++)
{
if(t[j]=='(')
{
m++;
k1=strlen(t1);
k2=strlen(t2);
if(k1<k2)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t1[m1++]='(';
t1[m1]='\0';
}
else if(t[j]=='[')
{
n++;
k3=strlen(t3);
k4=strlen(t4);
if(k3<k4)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t3[m4++]='[';
t3[m3]='\0';
}
else if(t[j]==')')
{
k1=strlen(t1);
k2=strlen(t2);
if(k1<k2)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m2=0,m1=0,m3=0,m4=0;
break;
}
else
t2[m2++]=')';
t2[m2]='\0';
m--;
}
else if(t[j]==']')
{
k3=strlen(t3);
k4=strlen(t4);
if(k3<k4)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t4[m4++]=']';
t4[m4]='\0';
n--;
}
}
if(check!=1&&m==0&&n==0)
printf("Yes\n");
else
{
m=0;
n=0;
m1=0;m2=0;m3=0;m4=0,k1=0,k2=0,k3=0,k4=0;
check=0;
continue;
}
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char t[200],t1[200],t2[200],t3[200],t4[200];
char t5[200];
int i,j,k,m=0,n=0,l,check=0,check1=0,k1=0,k2=0,m1=0,m2=0,m3=0,m4=0;
char s[200];
int k3,k4;
long p;
p=atol(gets(s));
for(l=0;l<p;l++)
{
gets(t);
i=strlen(t);
if(i==0)
{
printf("Yes\n");
continue;
}
for(j=0;j<i;j++)
{
if(t[j]=='(')
{
m++;
k1=strlen(t1);
k2=strlen(t2);
if(k1<k2)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t1[m1++]='(';
t1[m1]='\0';
}
else if(t[j]=='[')
{
n++;
k3=strlen(t3);
k4=strlen(t4);
if(k3<k4)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t3[m4++]='[';
t3[m3]='\0';
}
else if(t[j]==')')
{
k1=strlen(t1);
k2=strlen(t2);
if(k1<k2)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m2=0,m1=0,m3=0,m4=0;
break;
}
else
t2[m2++]=')';
t2[m2]='\0';
m--;
}
else if(t[j]==']')
{
k3=strlen(t3);
k4=strlen(t4);
if(k3<k4)
{
printf("No\n");
check=1;
k1=0;k2=0;k3=0;k4=0,m=0,n=0,m1=0,m2=0,m3=0,m4=0;
break;
}
else
t4[m4++]=']';
t4[m4]='\0';
n--;
}
}
if(check!=1&&m==0&&n==0)
printf("Yes\n");
else
{
m=0;
n=0;
m1=0;m2=0;m3=0;m4=0,k1=0,k2=0,k3=0,k4=0;
check=0;
continue;
}
}
}
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
Using STL
Yeah... using STL is possible,but when I submitted my code using STL,I got a very2 slow AC..... Maybe using Bruteforce could reduce the runtime...
Hope it helps...

I think this problem can be solved easily if you use stack.
- scan the string from left to right
- if find '(' or '[' push them into your stack
- if find ')' or ']' and the top of the stack is not they match then break from
the looping the answer is "No". Otherwise pop from the stack
- after you finished scan the string but the stack is not empty then the answer
is "No", otherwise "Yes".
I hope you understand what I mean and can get AC.

- scan the string from left to right
- if find '(' or '[' push them into your stack
- if find ')' or ']' and the top of the stack is not they match then break from
the looping the answer is "No". Otherwise pop from the stack
- after you finished scan the string but the stack is not empty then the answer
is "No", otherwise "Yes".
I hope you understand what I mean and can get AC.





-
- Experienced poster
- Posts: 101
- Joined: Wed May 04, 2005 4:33 pm
- Location: Tangerang, Banten, Indonesia
- Contact:
Here are some input for your program:
The output should be:
Code: Select all
4
([])
(([()])))
([()[]()])()
[(])
Code: Select all
Yes
No
Yes
No
Last edited by Roby on Sun Nov 27, 2005 5:04 pm, edited 1 time in total.
-
- Experienced poster
- Posts: 101
- Joined: Wed May 04, 2005 4:33 pm
- Location: Tangerang, Banten, Indonesia
- Contact:
Yes, the input wouldn't have '{' and '}'.
I've mistaken for read the problem description (lucky me, my code give me AC). And there also wouldn't any spaces given in the input. Sorry... 

Code: Select all
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

-
- Experienced poster
- Posts: 122
- Joined: Sun Nov 13, 2005 10:25 am
- Location: Taiwan
673 .... WA , please help me
What's wrong?
#include <stdio.h>
#include <string.h>
int main()
{
int a,b,e,f,g,h;
#include <stdio.h>
#include <string.h>
int main()
{
int a,b,e,f,g,h;
Last edited by Wei-Ming Chen on Tue Jan 03, 2006 1:38 pm, edited 1 time in total.