10293 - Word Length and Frequency

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

Moderator: Board moderators

turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
Location: (CSE,DU) Dhaka,Bangladesh

Post by turcse143 » Fri Feb 29, 2008 8:56 pm

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.
''I want to be most laziest person in the world''

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

No newlines at the end - I think...

Post by zyxw » Mon Jul 14, 2008 4:50 pm

My program doesn't handle newlines after last block of input. But got AC :lol:
I am not totally useless, because I can still be used as a bad example :P

Examiner
New poster
Posts: 27
Joined: Thu Feb 19, 2004 1:19 pm

Re: 10293 - Word Length and Frequency

Post by Examiner » Wed May 22, 2013 6:01 pm

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 (;;);
}

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

Re: 10293 - Word Length and Frequency

Post by uDebug » Wed Mar 19, 2014 8:46 am

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

Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 102 (10200-10299)”