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.
10293 - Word Length and Frequency
Moderator: Board moderators
No newlines at the end - I think...
My program doesn't handle newlines after last block of input. But got AC ![:lol:](./images/smilies/icon_lol.gif)
![:lol:](./images/smilies/icon_lol.gif)
I am not totally useless, because I can still be used as a bad example ![:P](./images/smilies/icon_razz.gif)
![:P](./images/smilies/icon_razz.gif)
Re: 10293 - Word Length and Frequency
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
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.Examiner wrote:There are extra lines after the last # line
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
}
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
}
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
}
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!?.,
#
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