Page 1 of 12

10324 - Zeros and Ones

Posted: Fri Jul 05, 2002 2:02 pm
by htl
Why does this code get RE?
[c]
#include<stdio.h>
#define SWAP(x,y,t) (t=x,x=y,y=t)
void main(void)
{
int n,x;
long start,end,t,y,data[1000][2],cnt;
char *buffer,temp;
buffer=(char *)malloc(sizeof(char)*1000000);
for(cnt=1;scanf("%s",buffer)!=EOF;cnt++)
{
scanf("%d",&n);
for(x=0;x<n;x++)
scanf("%ld %ld",&data[x][0],&data[x][1]);
printf("Case %d:\n",cnt);
for(x=0;x<n;x++)
{
start=data[x][0],end=data[x][1];
if(start>end)
SWAP(start,end,t);
for(temp=buffer[start],y=start;y<=end;y++)
if(buffer[y]!=temp)
break;
if(y<=end)
printf("No\n");
else
printf("Yes\n");
}
}
free(buffer);
}
[/c]

Posted: Fri Jul 05, 2002 3:49 pm
by 10153EN
It because number of cases maybe (and probably) > 1000, so there's an overflow in array data. Try a larger size and it should not have RE. (but your algorithm probably will get TLE :wink: )

p.s. actually you have no need to store all the pair of starting and ending positions~

Posted: Sat Jul 06, 2002 3:39 am
by htl
I rewrite the code as below
[c]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SWAP(x,y,t) (t=x,x=y,y=t)
void main(void)
{
int n,x,*table;
long start,end,t,cnt,y;
char *buffer,temp,c;
table=(int *)malloc(sizeof(int)*1000000);
buffer=(char *)malloc(sizeof(char)*1000000);
for(cnt=1;;cnt++)
{
for(x=0;(c=getchar())!='\n';x++)
if(c==EOF)
exit(0);
else
buffer[x]=c;
buffer[x]='\0';
if(!strlen(buffer))
exit(0);
printf("Case %d:\n",cnt);
for(y=0;y<strlen(buffer);y++)
{
table[y]=0;
if(y==0)
table[0]=buffer[0]-'0';
else
table[y]=buffer[y]-'0'+table[y-1];
}
scanf("%d",&n);
for(y=0;y<n;y++)
{
scanf("%ld %ld\n",&start,&end);
if(start>end)
SWAP(start,end,t);
if(start==0)
{
if(table[end]==0 || table[end]==end+1)
printf("Yes\n");
else
printf("No\n");
}
else
{
if(table[end]-table[start-1]==0 || table[end]-table[start-1]==end-start+1)
printf("Yes\n");
else
printf("No\n");
}
}
}
free(buffer);
}
[/c]

But I tested the code with '10324 < a.in'
a.in:
0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62

1
1
0 0
And the code replies
Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes
But between case 2 and case 3 there's a newline.So the case 3 shouldn't be print out. How do I solve the problem?

10324

Posted: Sat Jul 06, 2002 3:41 am
by KE4MTG
Hey guys, whats up.
This is my first program with this online judge thing so I have to get used to it. I'm getting this compiler error message:
/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
... And here is my code : [cpp]//@BEGIN_OF_SOURCE_CODE
/* @JUDGE_ID: 20782HK 10324 C++ */
#include <stdio.h>
void process(char string[1000000], int pos1, int pos2)
{ bool answer = true;
if(pos1 > pos2)
{ int temp = pos2;
pos2 = pos1;
pos1 = temp;
}
for(int i = pos1; i <= pos2-1; i++)
{ if(string != string[i+1])
answer = false;
}
if(answer)
printf("Yes\n");
else if(!answer)
printf("No\n");
}

int main()
{ char string[1000000];
int count = 1,num;
while(scanf("%s", &string)==1)
{ printf("Case %d:\n",count);
scanf("%d",&num);
int x, y;
for(int i = 0; i < num; i++)
{ scanf("%d",&x);
scanf("%d",&y);
process(string,x,y);
}
count++;
}
return 0;
}
//@END_OF_SOURCE_CODE[/cpp]

Any suggestions would be appreciated :wink:
Thanx

Posted: Sat Jul 06, 2002 6:42 am
by 10153EN
If because of the following lines:
//@BEGIN_OF_SOURCE_CODE
//@END_OF_SOURCE_CODE
Don't use // to do comment, use /* and */ instead

Posted: Sat Jul 06, 2002 7:39 am
by KE4MTG
Thanx so much. I appreciate it. But now I get this stupid compiler message regarding the #include thingy
00933964_24.c:50: `#include' expects "FILENAME" or <FILENAME>
All i included was stdio.h
ARGGGHH This compiler makes me so upset =/
Anyway Thanx again.

Posted: Sat Jul 06, 2002 8:30 am
by 10153EN
I copied and pasted your code and to the judge with no compile error.

Could you checked once more the include statement or give me to have a look at your updated code? via Private Message or post here is both ok~

Posted: Sat Jul 06, 2002 6:28 pm
by KE4MTG
I don't know what is wrong. I copied and pasted from this thread and submitted it but i still get the #include compiler error. Here is my code. It is the exact same as above except without the // .

[cpp]/* @JUDGE_ID: 20782HK 10324 C++ */
#include <stdio.h>
void process(char string[1000000], int pos1, int pos2)
{ bool answer = true;
if(pos1 > pos2)
{ int temp = pos2;
pos2 = pos1;
pos1 = temp;
}
for(int i = pos1; i <= pos2-1; i++)
{ if(string != string[i+1])
answer = false;
}
if(answer)
printf("Yes\n");
else
printf("No\n");
}

int main()
{ char string[1000000];
int count = 1,num;
while(scanf("%s", &string)==1)
{ printf("Case %d:\n",count);
scanf("%d",&num);
int x, y;
for(int i = 0; i < num; i++)
{ scanf("%d",&x);
scanf("%d",&y);
process(string,x,y);
}
count++;
}
return 0;
}
[/cpp]

Don't know why it keeps giving me a compiler message. I hope my code is correct in case i ever fix the compiler errors so I can quit working on this stupid problem. Is there a compiler i could download that is similar to the judge's?? Anyway thanks a lot.

Posted: Sat Jul 06, 2002 6:55 pm
by 10153EN
I copied and pasted your codes and submitted. It did not return compile error.....

What means do you submit your program by?

10324 -- Time Limit Exceed

Posted: Sat Jul 06, 2002 7:20 pm
by htl
Why does this code get TLE? I think my algorithm is fast enough. It should be the data reading problem.
[c]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SWAP(x,y,t) (t=x,x=y,y=t)
void main(void)
{
int n,x,*table;
long start,end,t,cnt,y;
char *buffer,temp,c;
table=(int *)malloc(sizeof(int)*1000000);
buffer=(char *)malloc(sizeof(char)*1000000);
for(cnt=1;;cnt++)
{
for(y=0;(c=getchar())!='\n';y++)
if(c==EOF)
exit(0);
else
buffer[y]=c;
buffer[y]='\0';
if(!strlen(buffer))
exit(0);
printf("Case %d:\n",cnt);
for(y=0;y<strlen(buffer);y++)
{
table[y]=0;
if(y==0)
table[0]=buffer[0]-'0';
else
table[y]=buffer[y]-'0'+table[y-1];
}
scanf("%d",&n);
for(y=0;y<n;y++)
{
scanf("%ld %ld",&start,&end);
if(start>end)
SWAP(start,end,t);
if(start==0)
{
if(table[end]==0 || table[end]==end+1)
printf("Yes\n");
else
printf("No\n");
}
else
{
if(table[end]-table[start-1]==0 || table[end]-table[start-1]==end-start+1)
printf("Yes\n");
else
printf("No\n");
}
}
c=getchar();
}
free(buffer);
}
[/c]

Posted: Sat Jul 06, 2002 8:05 pm
by KE4MTG
This is making me mad. I am using AOL, which is bad I know, but I sent it to a yahoo account i made and it did not add anything to the e-mail. Now I tried it with yahoo, and it sends and doesn't return an #include message but yahoo mail adds a little advertisement at the bottom of every e-mail which sucks. So...it is probably my Internet provider. Got any suggestions about this problem. Man you are being a great help to me, thanks

Posted: Sat Jul 06, 2002 8:11 pm
by 10153EN
Oh... you can re-add the BEGIN_OR_SOURCE_CODE and END_OF_SOURCE_CODE by bracketing with /* and */ to fix the *advertisement* problem with your email client.

Posted: Sat Jul 06, 2002 8:15 pm
by KE4MTG
HAHAHAHAHA....TLE now. 30 seconds. That's funny. Well man thanks a lot. This is a harder program than i thought.

Posted: Sat Jul 06, 2002 8:17 pm
by KE4MTG
O yea one more question.... Is it okay if I output the answers before the judge is finished inputing all the data?? or will that mess it up?

Posted: Sat Jul 06, 2002 8:22 pm
by 10153EN
No need~ :)

Actually you have no need to store up all the input data before outputing your answers. Just loop number_of_case times for your input and output procedure~