12945 - Magicka Spell   

Description

There’re N magicka spells. Each spell is composed by several digits 1~9.

123, 7 are valid spells, but a12, 0, 073 are not.

The damage of the spell is the decimal number represented by itself.

We want to get largest damage by sort the digits of spell from large to small.
Let’s call the sorted spell as critical spell.

For example, 7731 is the critical spell of 1377, 7317, 3717, … .

Your task is to find all kinds of critical spells of N spells, and output them without repetition in decreasing order by their damages.

Hint

You may consider the following algorithm:

  1. Read N Spells as strings
  2. For each si, sort the digits of it to obtain its critical spell.
  3. Sort critical spells by their damages.
    You can compare the length first.
    If not same, output their relation. If equal, compare their digits.
  4. Output all kinds of critical spells without repeat.
    You should check whether each critical spell has been printed or not.
    If not, print it.

For sorting, you can use the following code:

char s[30]; 
…
int len = strlen(s);
for (int i = 0; i < len; i++) {
    for (int j = 0; j < len - i - 1; j++) {
        if (s[j] < s[j + 1]) {
            /*swap s[j] and s[j+1]*/
        }
    }
}

Testcases

Testcase 1 ~ 5:   |si| = |sj| for all i, j. |si| ≤ 8, for all i
Testcase 6 ~ 8:   |si| ≠ |sj| for some i, j. |si| ≤ 8, for all i
Testcase 9 ~ 10: |si| ≠ |sj| for some i, j. |si| ≤ 50, for all i

 

Input

There’s N on the first line.
There’s a spell si on the each of following N lines.

It’s guaranteed that:

  1. 1 ≤ N ≤ 100
  2. 1 ≤ |si| ≤ 50
  3. Each charactor of si ‘1’~‘9’

Output

Print the critical spells without repetition in decreasing order on each line.
Remember ‘\n’ on the end of each line.

Sample Input  Download

Sample Output  Download

Tags




Discuss