forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKingSort.html
More file actions
23 lines (20 loc) · 6.93 KB
/
Copy pathKingSort.html
File metadata and controls
23 lines (20 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td><p>
Every good encyclopedia has an index. The entries in the index are usually sorted in alphabetic order. However, there are some notable exceptions. In this task we will consider one such exception: the names of kings.
</p>
<p>
In many countries it was common that kings of the same name received ordinal numbers. This ordinal number was written as a Roman numeral and appended to the actual name of the king.
For example, "Louis XIII" (read: Louis the thirteenth) was the thirteenth king of France having the actual name Louis.
</p>
<p>
In the index of an encyclopedia, kings who share the same name have to be sorted according to their ordinal numbers. For example, Louis the 9th should be listed after Louis the 8th.
</p>
<p>
You are given a vector <string> <b>kings</b>. Each element of <b>kings</b> is the name of one king. The name of each king consists of his actual name, a single space, and a Roman numeral. Return a vector <string> containing the names
rearranged into their proper order: that is, the kings have to be in ascending lexicographic order according to their actual name, and kings with the same name have to be in the correct numerical order.
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>KingSort</td></tr><tr><td>Method:</td><td>getSortedList</td></tr><tr><td>Parameters:</td><td>vector <string></td></tr><tr><td>Returns:</td><td>vector <string></td></tr><tr><td>Method signature:</td><td>vector <string> getSortedList(vector <string> kings)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>    </td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>The Roman numerals for 1 through 10 are I, II, III, IV, V, VI, VII, VIII, IX, and X.</td></tr><tr><td align="center" valign="top">-</td><td>The Roman numerals for 20, 30, 40, and 50 are XX, XXX, XL, and L.</td></tr><tr><td align="center" valign="top">-</td><td>The Roman numeral for any other two-digit number less than 50 can be constructed by concatenating the numeral for its tens and the numeral for its ones. For example, 47 is 40 + 7 = "XL" + "VII" = "XLVII".</td></tr><tr><td align="center" valign="top">-</td><td>Standard string comparison routines give the correct ordering for the actual names of kings.</td></tr><tr><td align="center" valign="top">-</td><td>Formally, given two different strings A and B we say that A is lexicographically smaller than B if either (A is a prefix of B) or (there is at least one index where A and B differ, and for the smallest such index the character in A has a lower ASCII value than the character in B).</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td>Each actual name of a king will be a string containing between 1 and 20 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each actual name will start by an uppercase letter ('A'-'Z').</td></tr><tr><td align="center" valign="top">-</td><td>Each other character in each actual name will be a lowercase letter ('a'-'z').</td></tr><tr><td align="center" valign="top">-</td><td><b>kings</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>kings</b> will have the form "ACTUALNAME ORDINAL", where ACTUALNAME is an actual name as defined above, and ORDINAL is a valid Roman numeral representing a number between 1 and 50, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The elements of <b>kings</b> will be pairwise distinct.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"Louis IX", "Louis VIII"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {"Louis VIII", "Louis IX" }</pre></td></tr><tr><td><table><tr><td colspan="2">Louis the 9th should be listed after Louis the 8th.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"Louis IX", "Philippe II"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {"Louis IX", "Philippe II" }</pre></td></tr><tr><td><table><tr><td colspan="2">Actual names take precedence over ordinal numbers.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"Richard III", "Richard I", "Richard II"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {"Richard I", "Richard II", "Richard III" }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"John X", "John I", "John L", "John V"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {"John I", "John V", "John X", "John L" }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"Philippe VI", "Jean II", "Charles V", "Charles VI", "Charles VII", "Louis XI"}</pre></td></tr></table></td></tr><tr><td><pre>Returns:
{"Charles V",
"Charles VI",
"Charles VII",
"Jean II",
"Louis XI",
"Philippe VI" }</pre></td></tr><tr><td><table><tr><td colspan="2">These are the French monarchs who ruled between 1328 and 1483.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"Philippe II", "Philip II"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {"Philip II", "Philippe II" }</pre></td></tr><tr><td><table><tr><td colspan="2">"Philippe" and "Philip" are distinct names, and "Philip" is lexicographically smaller than "Philippe".</td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>