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

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

Post by SilVer DirectXer »

o, i change my coding to a Full C program , not using classes and using scanf and printf instead of using cin cout, and finally i got a AC!

really thanks. :wink:

zzylhy
New poster
Posts: 6
Joined: Mon May 19, 2003 1:56 pm

673

Post by zzylhy »

#include<iostream.h>
#include<stdio.h>

int main()
{
int n,i;
char s[130],ch,flag;
cin>>n;
while(n>0)
{
n--;
i=0;
flag=1;
while(1)
{
ch=getchar();
if(ch==10)
break;
else
if((ch=='(')||(ch=='['))
{
i++;
s=ch;
}
else
{
if(i==0)
{
i=1;
ch=getchar();
if(ch==10)
break;
}

if(((ch==']')&&s=='[')||((ch==')')&&(s=='(')))
{
i--;

}
else
{
i=1;
}

}
}
if(i==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

Help,why always WA?

udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

673..........getting RTE RTE RTE

Post by udvat »

plz check my code n let me know whats the fault? :oops:



[cpp]
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

void main()
{

char C;
char arr[200]={0};
char x[200]={0};
int N;
int i,y,l;

scanf("%d",&N);
getchar();

y=0;

while(N--)
{

y=0;


gets(x);


l=strlen(x);

if(l==0)
{printf("Yes\n");
continue;}


if(l%2)
{printf("No\n");
continue;}


for(i=0;i<=l;i++)
{


C=x;


if(C=='\0')
{
if(!strcmp(arr,NULL))
{
printf("Yes\n");
}

else
{
printf("No\n");

}
break;
}




if(C=='(' || C=='[')
{
arr[y++]=C;
arr[y]='\0';
}

else if(C==')')
{
if(y==0)
{
arr[y++]=C;
arr[y]='\0';
}

else
{
if(arr[--y]!='(')
{
arr[y++]=C;
arr[y]='\0';
}

else
arr[y]='\0';

}


}


else if (C==']')
{
if(y==0)
{
arr[y++]=C;
arr[y]='\0';
}

else
{
if(arr[--y]!='[')
{
arr[y++]=C;
arr[y]='\0';
}

else
arr[y]='\0';


}

}




}

}




}



[/cpp]

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Re: 673..........getting RTE RTE RTE

Post by UFP2161 »

udvat wrote:[cpp]
if(!strcmp(arr,NULL))
[/cpp]
I'm pretty certain you can't pass NULL to strcmp. If you want the empty string, use "" instead.

udvat
New poster
Posts: 29
Joined: Thu Aug 07, 2003 9:56 pm

thanxxxxxxxx friend

Post by udvat »

I got accepted!!!!!!!I really dnt know strcmp cant work with NULL.thanks again :D :D :D :D :D :D

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

Like all functions in <string.h>, it requires that the character array ends in '\0'. However, if you pass NULL as a pointer to any of those functions, the function will attempt to dereference it first so it can check for '\0' and thus the end of the string, but since you passed NULL, it can't dereference NULL, thus, segmentation fault.

User avatar
Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

673 Need Help

Post by Sarmento »

It seems to be working just fine... till the moment I submit it... Can anyone give me a clue why?

Code: Select all

[c]
include <stdio.h>
#include <string.h>

int n, i,j, length;
char str[130];


void rem_chars(int a){
	int x;
	char ret[length];
	for (x = 0; x < length; x++){
		if (x < a){
			ret[x] = str[x];
		}
		else if (x > a+1){
			ret[x-2] = str[x];
		}
	}
	ret[x-2]='\0';
	strcpy(str,ret);
	length = strlen(str);
}

int main(){
	scanf("%d",&n);
	for(i = 0; i < n; i++){
		scanf("%s", str);
		length = strlen(str);
		for (j = 0; j <= length; j++){
			if (length == 0){
				printf("Yes\n");
				break;
			}
			else{
				if ((str[j] == ')' && str[j-1] == '(') || (str[j] == ']' && str[j-1] == '[')){
					rem_chars(j-1);
					j = -1;
				}
				else if((str[j] == ')' && str[j-1] != '(') || (str[j] == ']' && str[j-1] != '[') || (j == length && length > 0)){
					printf("No\n");
					break;
				}
			}
		}
	}
	return 0;	
}
[/c]
-----------------
Jo

Dmytro Chernysh
Experienced poster
Posts: 146
Joined: Sat Apr 26, 2003 2:51 am

Post by Dmytro Chernysh »

Well, your code is not that clear... Try to think (and implement :-) a stack...
I can give you my AC program, if you really got stuck.

Joe
New poster
Posts: 6
Joined: Fri Aug 08, 2003 2:06 pm
Location: Egypt

673

Post by Joe »

why do I get Runtime error in problem 673? Please help.
[cpp]#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
char b[129];

void init(){
for(int i=0;i<129;i++)
b='\0';
}
class stack{

public:
int index;
stack(){index=0;}
int data[129];
void push(int n){data[index++]=n;}
int pop(){return(index==0)?-5:data[--index];}
};

int main()
{
stack s;
bool flag;
int res;
int c,i=0;
init();
scanf("%d\n",c);
for (int k=0;k<c;k++){
init();
i=0;
scanf("%s",b);
flag=true;
while(i<strlen(b)){
switch (b){
case '(': s.push(0);
break;
case '[': s.push(1);
break;
case ')':{
res=s.pop();
if (res!=0) flag=false;
}
break;
case ']':{
res=s.pop();
if (res!=1) flag=false;
}
break;
}
i++;
}
if (!flag || s.index!=0 || b!='\0') cout<<"No\n";
else cout<<"Yes\n";
}
return 0;
}[/cpp]
thnx

Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

Post by Sajid »

umm, check with few large array size, like 130 or 132.
Sajid Online: www.sajidonline.com

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

673Parentheses Balance

Post by AaronWu »

I have tried many inputs as are mentioned in the former topics but I still have no idea about WA with my code. Could anybody spare some time checking with my code and tell me the matter with my code? Thank you in advance.
:o
[cpp]
#include <iostream.h>
#include <stdio.h>
int solve(char *);
int main()
{
long int n,count ;
int result;
char *chs = new char[129];
cin>>n;
for(count=0;count<n;count++)
{
gets(chs);
result = solve(chs);
if(result==1)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
..........

}[/cpp]
Last edited by AaronWu on Wed Jan 28, 2004 6:41 am, edited 1 time in total.

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

The problem is when you read the first line (n) ... using cin >> n, I guess it won't advance to the next line or something like that ... causing problems on the next cases ... try to use gets(), followed by atoi() to obtain n ... you should get AC after that.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

turuthok wrote: I guess it won't advance to the next line or something like that

-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above? :roll:
Looking forward to reply~
To save time is to lengthen life.

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

turuthok wrote: I guess it won't advance to the next line or something like that

-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above? :roll:
Looking forward to reply~
To save time is to lengthen life.

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

turuthok wrote: I guess it won't advance to the next line or something like that

-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above? :roll:
Looking forward to reply~
To save time is to lengthen life.

Post Reply

Return to “Volume 6 (600-699)”