#K1906. Rearrange Sentences
Rearrange Sentences
Rearrange Sentences
You are given multiple lines of input. Each line (except the terminating line) begins with an integer n that represents the number of words that follow. Your task is to rearrange the words in each line in alphabetical order and output the modified sentence.
The input terminates with a line containing a single '0'.
Note: When sorting, use standard lexicographical order. For example, given the sentence "5 hello world this is test", you should extract the 5 words, sort them to get "hello is test this world", and then output the sorted sentence.
The mathematical interpretation of the sorting can be summarized using LaTeX as follows:
$$\text{Let } S = \{w_1, w_2, \dots, w_n\} \text{ be the set of words. Then, the output is } sort(S)$$
inputFormat
The input consists of several lines. Each line starts with an integer n followed by n words separated by spaces. The input terminates with a line containing a single "0", which should not be processed.
Input is read from standard input (stdin).
outputFormat
For each valid input line, output a single line containing the n words arranged in alphabetical order, separated by a single space. Output to standard output (stdout).
## sample5 hello world this is test
3 sorting words automatically
4 programming in a nutshell
0
hello is test this world
automatically sorting words
a in nutshell programming
</p>