Page 2 of 2

Posted: Fri Feb 29, 2008 8:56 pm
by turcse143
by using character array. u can get RTE with the signal of

-- invalid memory reference
instead of it u can use %c for taking input
character by character
check '#' & print the output.

hope it helps.

No newlines at the end - I think...

Posted: Mon Jul 14, 2008 4:50 pm
by zyxw
My program doesn't handle newlines after last block of input. But got AC :lol:

Re: 10293 - Word Length and Frequency

Posted: Wed May 22, 2013 6:01 pm
by Examiner
There are extra lines after the last # line, as the following program reaches the time limit.

Code: Select all

#include <iostream>
using namespace std;

int main() {
    string s;
    while (getline(cin, s));
    if (s[0] != '#')
        for (;;);
}

Re: 10293 - Word Length and Frequency

Posted: Wed Mar 19, 2014 8:46 am
by uDebug
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