483 – Word Scramble – UVA – ACM

Analysis :


 

Solved By:                          stevenha(Sayutee)
Theory Difficulty:            easy
Coding Difficulty:            easy
Algorithms Used:             strings
Solution Description:

For this kind of problem (which never states the maximum size of the lines), never ever read one whole line then start processing! Most likely we do not have enough buffer to do that. Instead, read character by character, buffer them into a small array, as soon as we encounter a space ‘ ‘ or other white spaces, quickly reprint these characters in reverse order, and then flush the buffer. This way, we will not get out of memory error :). The pseudo code will be something like this:

Initially set word to (“”) (empty string)

While not end-of-input
Set c = next character in the input stream
If c = whitespace or punctuation then
// word is complete
reverse word
output word
Else
Add c to word
End-If

Output c
end-while


 

Solved By:                          peter
Theory Difficulty:            trivial
Coding Difficulty:            trivial
Algorithms Used:             strings
Solution Description:

Take the input line by line.

Retokenize it, reversing each word with a for loop and outputting in the same order.

Print out the modified line.


Solved By:                           wesley
Theory Difficulty:             easy
Coding Difficulty:             easy
Algorithms Used:              strings
Solution Description:

Iterate through each line. When you encounter a non-whitespace character, set an index variable to the index of that character. When you encounter a whitespace character (or end of line) and the index has been set, then reverse the characters between the set index and the current index – 1.

In Java you can make quick work of this problem using StringBuffer.

I handled space, newline, tab (\t), and carriage return (\r) as whitespace. I’m not sure if tabs or carriage returns actually show up in the input.


Code: