Page 7 of 7

Re: 10106 -Product

Posted: Mon Feb 24, 2014 2:07 pm
by uDebug
Replying to this thread so I can follow it. Thanks.

Re: 10106 -Product

Posted: Sat Apr 26, 2014 9:47 pm
by Rika71
why WA??? please help.thanks in advance

Code: Select all

Removed after AC

Re: 10106 -Product

Posted: Mon Apr 28, 2014 11:19 am
by uDebug
Rika71 wrote:why WA??? please help.thanks in advance
So, couple things

(1) Use code tags to make your code more readable.
(2) Change

Code: Select all

while(scanf("%s%s",&s1,&s2)==2)
to

Code: Select all

while(scanf("%s%s",s1,s2) != EOF)
(3) Change

Code: Select all

int main(void)
to

Code: Select all

int main()
(4) Move

Code: Select all

char s1[1000],s2[1000],s3[40000][40000],s4[100000];
inside main.
(5) Try running your code on the sample input. It's generally a good idea to do so before submitting your program. Your code seg faults.

Re: 10106 -Product

Posted: Mon Apr 28, 2014 7:06 pm
by brianfry713
v1n1t,

See:
http://www.cplusplus.com/reference/cstdio/scanf/
I prefer using:
while(scanf("%s%s", s1, s2) == 2)
instead of:
while(scanf("%s%s", s1, s2) != EOF)
Try this input to see the difference:

Code: Select all

1 2 3
Your third point is equivalent and just a matter of style.

Large arrays should be declared globally, static, or dynamic so they go on the heap instead of the stack. Rika71, why does s3 need to be that large?

Re: 10106 -Product

Posted: Mon Apr 28, 2014 8:08 pm
by uDebug
brianfry713 wrote: See:
http://www.cplusplus.com/reference/cstdio/scanf/
I prefer using:
while(scanf("%s%s", s1, s2) == 2)
instead of:
while(scanf("%s%s", s1, s2) != EOF)
Gotcha. Thanks for sharing. I didn't know this.
Try this input to see the difference:

Code: Select all

1 2 3
Will do.
Your third point is equivalent and just a matter of style.
Again, thank you for teaching me.
Large arrays should be declared globally, static, or dynamic so they go on the heap instead of the stack.

Agreed.

But for some reason, I couldn't get Rika71's code to compile unless I moved those arrays onto the stack. Could you compile and run as they posted it on the forum? If so, what did you do?

Thanks again for all the pointers.

Re: 10106 -Product

Posted: Tue Apr 29, 2014 9:52 pm
by brianfry713
On my machine, Rika71's code compiles and runs on the sample input correctly as he or she wrote it. What was your compile error v1n1t? Were you not able to allocate enough memory for s3? However if I move those large arrays inside of main then I get a seg fault on execution.

Try input:

Code: Select all

1063805700
972401858
Output should be:

Code: Select all

1034446639230990600

Re: 10106 -Product

Posted: Wed Apr 30, 2014 7:24 am
by uDebug
I did look at this but found no mention of what the "2" (specifically) is for. Is this related to the line?
On success, the function returns the number of items of the argument list successfully filled.
So, if we were reading in 3 inputs, for example, we'd replace the "2" by "3"?
I prefer using:
while(scanf("%s%s", s1, s2) == 2)
instead of:
while(scanf("%s%s", s1, s2) != EOF)
Try this input to see the difference:

Code: Select all

1 2 3
OK. I got around to doing this. Here's what I wrote as a test

Code: Select all

#include<bits/stdc++.h>

using namespace std;

#define SIZE 1000

int main() {
	
	char s1[SIZE], s2[SIZE];

	while(scanf("%s%s", s1, s2) != EOF) {
		printf("s1: %s and s2: %s\n", s1, s2);
	}

	return 0;
}
When I ran this code on the input you provided, it output

Code: Select all

s1: 1 and s2: 2
s1: 3 and s2: 2
I have no idea why.

However, the same code with the while loop line replaced per your preference, it output

Code: Select all

s1: 1 and s2: 2
which is what I'd expected the first piece of code to do. So now, I'm confused.
On my machine, Rika71's code compiles and runs on the sample input correctly as he or she wrote it.
Oh great. Thanks for sharing.
What was your compile error v1n1t? Were you not able to allocate enough memory for s3?
So, the code compiles and builds fine. But when I run it on the following (sample) input

Code: Select all

12
12
2
222222222222222222222222
I get the following mysterious message

Code: Select all

Killed
And nothing more at all. Not sure what's going on.

Re: 10106 -Product

Posted: Wed Apr 30, 2014 8:02 pm
by brianfry713
Yes, see the return value of scanf:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If you're reading two strings at a time using scanf and the input has an even number of strings, then testing for a return value of two or not EOF will have the same result. However in the case of an odd number of strings, the last call to scanf will return 1 and the second string is unchanged.


Your machine is probably Killing that code because of the memory it's trying to allocate:
char s3[40000][40000] alone is 1,600,000,000 bytes.

Re: 10106 -Product

Posted: Wed Apr 30, 2014 8:24 pm
by Rika71
brianfry713 wrote:On my machine, Rika71's code compiles and runs on the sample input correctly as he or she wrote it. What was your compile error v1n1t? Were you not able to allocate enough memory for s3? However if I move those large arrays inside of main then I get a seg fault on execution.
A profusion of thanks to brianfry713.
I just fixed my code for your sample & got AC.
[by the way I am he :D ]