Tidying the search term list
Statement
The search box of a secondhand bookshop took in N search terms over one day. Every term was logged in the order it arrived, so the same term can sit in the log several times. Each search term consists only of lowercase English letters.
The shop owner wants to tidy this log. The method is the following. First, keep one copy of each term and delete the rest. Then lay the terms out from the shortest one in order. Among terms of the same length, the one that comes earlier in lexicographic order goes first.
Given the N search terms, write a program that prints the tidied result, one term per line.
For example, suppose N=6 search terms arrived as in the following table.
| Order | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Search term | but | i | wont | hesitate | no | more |
The length of each search term is the following.
| Search term | but | i | wont | hesitate | no | more |
|---|---|---|---|---|---|---|
| Length | 3 | 1 | 4 | 8 | 2 | 4 |
Laying them out from the shortest one puts i, no and but in front, in that order. Next come the two terms of length 4, wont and more, and more goes first because it comes earlier in lexicographic order. Last is hesitate, with length 8. So the tidied result is the following.
| Order | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Search term | i | no | but | more | wont | hesitate |
In this example no term arrived twice. Had a term arrived two or more times, the tidied result would hold it only once. The number of printed lines can therefore be smaller than the number of terms that arrived.
Input and output examples
Example 1
Input
6 but i wont hesitate no more
Output
i no but more wont hesitate
Example 2
Input
2 cba ab
Output
ab cba
Example 3
Input
40 bbcab baabbbbb ba bccab aabcb aabacc cbaa bcbabcab caccacaa bcaa cbbcb bccbbbb b bb cbbbaaca accbaac cbaacacc bbcca caacbbc bcc cabca cabcc bb bcbbc b bbbaacc caaacccc baaa aabc bbaaa bcbcaabc babcac bbbaaab bbbbbbb ccaca bcbca b accaa abcaaac aaabaac
Output
b ba bb bcc aabc baaa bcaa cbaa aabcb accaa bbaaa bbcab bbcca bcbbc bcbca bccab cabca cabcc cbbcb ccaca aabacc babcac aaabaac abcaaac accbaac bbbaaab bbbaacc bbbbbbb bccbbbb caacbbc baabbbbb bcbabcab bcbcaabc caaacccc caccacaa cbaacacc cbbbaaca
Input
The first line gives the number of search terms N that arrived. N is a natural number from 1 to 20,000.
The next N lines give each search term, one per line, in the order it arrived. Each search term consists only of lowercase English letters, and its length is from 1 to 50. The same search term can be given several times.
Output
Print the tidied search terms, one per line. The same term is printed only once, so the number of printed lines can be smaller than N. Do not add a separate blank line after the last search term.
Requirements
- Read the input from standard input and write the result to standard output
- Keep one copy of each search term
- Print the shorter search terms first, and print terms of the same length in lexicographic order
- Print one search term per line
- Do not add a separate blank line after the last line
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 cba ab
ab cba
40 bbcab baabbbbb ba bccab aabcb aabacc cbaa bcbabcab caccacaa bcaa cbbcb bccbbbb b bb cbbbaaca accbaac cbaacacc bbcca caacbbc bcc cabca cabcc bb bcbbc b bbbaacc caaacccc baaa aabc bbaaa bcbcaabc babcac bbbaaab bbbbbbb ccaca bcbca b accaa abcaaac aaabaac
b ba bb bcc aabc baaa bcaa cbaa aabcb accaa bbaaa bbcab bbcca bcbbc bcbca bccab cabca cabcc cbbcb ccaca aabacc babcac aaabaac abcaaac accbaac bbbaaab bbbaacc bbbbbbb bccbbbb caacbbc baabbbbb bcbabcab bcbcaabc caaacccc caccacaa cbaacacc cbbbaaca
6 but i wont hesitate no more
i no but more wont hesitate
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.