10106 - Product
Moderator: Board moderators
Re: 10106 -Product
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.
Re: 10106 -Product
So, couple thingsRika71 wrote:why WA??? please help.thanks in advance
(1) Use code tags to make your code more readable.
(2) Change
Code: Select all
while(scanf("%s%s",&s1,&s2)==2)
Code: Select all
while(scanf("%s%s",s1,s2) != EOF)
Code: Select all
int main(void)
Code: Select all
int main()
Code: Select all
char s1[1000],s2[1000],s3[40000][40000],s4[100000];
(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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10106 -Product
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:
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?
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
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!
Re: 10106 -Product
Gotcha. Thanks for sharing. I didn't know this.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)
Will do.Try this input to see the difference:Code: Select all
1 2 3
Again, thank you for teaching me.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.
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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10106 -Product
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:
Output should be:
Try input:
Code: Select all
1063805700
972401858
Code: Select all
1034446639230990600
Check input and AC output for thousands of problems on uDebug!
Re: 10106 -Product
I did look at this but found no mention of what the "2" (specifically) is for. Is this related to the line?brianfry713 wrote: See:
http://www.cplusplus.com/reference/cstdio/scanf/
So, if we were reading in 3 inputs, for example, we'd replace the "2" by "3"?On success, the function returns the number of items of the argument list successfully filled.
OK. I got around to doing this. Here's what I wrote as a testI 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
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;
}
Code: Select all
s1: 1 and s2: 2
s1: 3 and s2: 2
However, the same code with the while loop line replaced per your preference, it output
Code: Select all
s1: 1 and s2: 2
Oh great. Thanks for sharing.On my machine, Rika71's code compiles and runs on the sample input correctly as he or she wrote it.
So, the code compiles and builds fine. But when I run it on the following (sample) inputWhat was your compile error v1n1t? Were you not able to allocate enough memory for s3?
Code: Select all
12
12
2
222222222222222222222222
Code: Select all
Killed
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10106 -Product
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.
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!
Re: 10106 -Product
A profusion of thanks to brianfry713.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.
I just fixed my code for your sample & got AC.
[by the way I am he
