10106 - Product

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10106 -Product

Post by uDebug » Mon Feb 24, 2014 2:07 pm

Replying to this thread so I can follow it. Thanks.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Rika71
New poster
Posts: 11
Joined: Sat Apr 26, 2014 9:42 pm

Re: 10106 -Product

Post by Rika71 » Sat Apr 26, 2014 9:47 pm

why WA??? please help.thanks in advance

Code: Select all

Removed after AC
Last edited by Rika71 on Wed Apr 30, 2014 8:36 pm, edited 2 times in total.

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10106 -Product

Post by uDebug » Mon Apr 28, 2014 11:19 am

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Mon Apr 28, 2014 7:06 pm

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?
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10106 -Product

Post by uDebug » Mon Apr 28, 2014 8:08 pm

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Tue Apr 29, 2014 9:52 pm

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
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10106 -Product

Post by uDebug » Wed Apr 30, 2014 7:24 am

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Wed Apr 30, 2014 8:02 pm

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.
Check input and AC output for thousands of problems on uDebug!

Rika71
New poster
Posts: 11
Joined: Sat Apr 26, 2014 9:42 pm

Re: 10106 -Product

Post by Rika71 » Wed Apr 30, 2014 8:24 pm

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 ]

Post Reply

Return to “Volume 101 (10100-10199)”