Page 2 of 17

Posted: Sun Apr 13, 2003 4:45 pm
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:

673

Posted: Tue May 20, 2003 12:38 pm
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?

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

Posted: Thu Aug 07, 2003 10:14 pm
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]

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

Posted: Fri Aug 08, 2003 5:01 am
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.

thanxxxxxxxx friend

Posted: Fri Aug 08, 2003 3:43 pm
by udvat
I got accepted!!!!!!!I really dnt know strcmp cant work with NULL.thanks again :D :D :D :D :D :D

Posted: Fri Aug 08, 2003 7:02 pm
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.

673 Need Help

Posted: Wed Oct 01, 2003 12:07 am
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]

Posted: Wed Oct 01, 2003 5:00 am
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.

673

Posted: Fri Nov 07, 2003 5:02 pm
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

Posted: Fri Nov 07, 2003 9:15 pm
by Sajid
umm, check with few large array size, like 130 or 132.

673Parentheses Balance

Posted: Fri Jan 16, 2004 8:20 am
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]

Posted: Fri Jan 16, 2004 10:31 am
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-

Posted: Fri Jan 16, 2004 12:56 pm
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~

Posted: Fri Jan 16, 2004 12:58 pm
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~

Posted: Fri Jan 16, 2004 1:01 pm
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~