Connection Status:
Competition Arena > TheBestName
SRM 381 · 2007-12-08 · by Vasyl[alphacom] · String Manipulation
Class Name: TheBestName
Return Type: String[]
Method Name: sort
Arg Types: (vector<string>)
Problem Statement

Problem Statement

As some of you may know, there is no name better than JOHN. Let's define the rules for comparing names. Each letter has a weight ('A' - 1, 'B' - 2, ..., 'Z' - 26). The weight of a name is the sum of the weights of all its letters. For example, the name MARK has weight 13 + 1 + 18 + 11 = 43.

When comparing two names, the one with the larger weight is considered better. In case of a tie, the one that comes earlier lexicographically is better. But there is one exception - the name JOHN is the best name of all.

You are given a String[] names, each element of which contains a single name. Sort the names from best to worst and return the sorted String[].

Constraints

  • names will contain between 1 and 50 elements, inclusive.
  • Each element of names will contain between 1 and 50 characters, inclusive.
  • Each element of names will contain only uppercase letters ('A'-'Z').
Examples
0)
{"JOHN", "PETR", "ACRUSH"}
Returns: {"JOHN", "ACRUSH", "PETR" }

PETR has weight 59, ACRUSH has weight 70 and JOHN has a weight of only 47. But nevertheless JOHN is the best name, ACRUSH takes second place and PETR is the last.

1)
{"GLUK", "MARGARITKA"}
Returns: {"MARGARITKA", "GLUK" }

MARGARITKA is definitely better than GLUK.

2)
{"JOHN", "A", "AA", "AAA", "JOHN", "B", "BB", "BBB", "JOHN", "C", "CC", "CCC", "JOHN"}
Returns: {"JOHN", "JOHN", "JOHN", "JOHN", "CCC", "BBB", "CC", "BB", "AAA", "C", "AA", "B", "A" }

AA and B both have the same weight, but AA is better as it comes earlier lexicographically. For the same reason, AAA is better than C and BBB is better than CC.

3)
{"BATMAN", "SUPERMAN", "SPIDERMAN", "TERMINATOR"}
Returns: {"TERMINATOR", "SUPERMAN", "SPIDERMAN", "BATMAN" }

Here are some superheroes sorted by their names.

4)
{"IVAN", "PETRO", "STEPAN", "OLEG", "IHOR", "VOLODYA"}
Returns: {"VOLODYA", "STEPAN", "PETRO", "IHOR", "IVAN", "OLEG" }

Submissions are judged against all 123 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TheBestName with a public method vector<string> sort(vector<string> names) · 123 test cases · 2 s / 256 MB per case

Submitting as anonymous