Examiner wrote:There are extra lines after the last # line
I really struggled with this for a couple hours and I'd like to share what I found so perhaps you don't have to.
Since I was reading the input line-by-line and was doing the parsing using C-style strings, I was employing the following piece of code to read lines
Code: Select all
while(scanf("%[^\n]%*c", inputStr) != EOF) {
// Do stuff
}
Note that "inputStr" is a C-style string.
However, the extra newlines after the last "#" was causing the program to go into an infinite loop. I then tried the following
Code: Select all
while(gets(inputStr)) {
// Do stuff
}
And this caused the program to seg fault.
Finally, after trial and error, I came up with these lines of code that worked. Note that "aLine" is an std string.
Code: Select all
while(getline(cin, aLine)) {
// If there's a newline, exit
if(aLine == "") {
break;
}
// Convert the std string to a C-style string
strcpy(inputStr, aLine.c_str());
// Do stuff
}
Here's some input / output I found useful during testing / debugging. Note that there are extra newlines at the end (just like on the judge's input). So be sure to include them when you're testing.
Thanks to
jan_holmes for their excellent test case.
Also, bear in mind that there's a newline after
every set of output -
even the last set.
Input:
Code: Select all
hyper!..Goal,terM,cham-
pion. Try it A!g!a!iA.N
Thank-
s
you.
G-
olly!
Thank-you-ver-
y
#
hyper!..Goal,terM,cham-
pion. Try it A!g!a!iA.N
Thank-
s
g-
reat you.
#
This is not so fun-
ny! Mr. Pi and I've definitely never seen Ma'm-
nizea and Shereen do this tour-de-force be-
fore. We swear!!!! Righ-
t!?.,
#
AC Output:
Code: Select all
1 4
2 2
3 2
4 2
5 2
6 1
8 1
12 1
1 4
2 2
3 2
4 2
5 2
6 1
8 1
2 6
3 4
4 3
5 4
6 1
7 1
8 1
10 1
11 1