13193 - GEC1506-TopN longest words (Advanced)
|
Time |
Memory |
| Case 1 |
1 sec |
32 MB |
| Case 2 |
1 sec |
32 MB |
| Case 3 |
1 sec |
32 MB |
| Case 4 |
1 sec |
32 MB |
| Case 5 |
1 sec |
32 MB |
| Case 6 |
1 sec |
32 MB |
Description
Given a number N and an article with each word separated by space,
you need to return top-N longest words.
Input
The input contains two parts:
- The first line of the input is a number that indicates N numbers of top-N.
- The remaining parts of the input (multiple lines) are the content of articles with each word/token separated by SPACE.
2
hello world ! welcome to gec course for exploring world of programming
good luck in the quiz
With these inputs, you need to sort the words based on the following criteria to find the top-N words:
- Word length in descending order. You can use function
len() , e.g. len('abc') # 3.
- If there are words having the same length, sort them again by the sum of the word's code using function
ord().
e.g. ord('a') # 97
- If two above values are the same for words, sort these words by their occurrence order.
Output
Print out multiple lines of distinct words in descending order.
Note that
- if N is more than the total distinct words, just print out the necessary lines of words.
e.g. if N=5, but there are only 4 words. You only need to print 4 lines of words as there is no way to print out 5 lines.
- if there exists a word that occurs more than 1 time, you don't need to remove the duplicated words when you are counting. That is, the output could have duplicate words.
Tags