Connection Status:
Competition Arena > Chivalry
SRM 213 · 2004-09-28 · by dplass · String Manipulation
Class Name: Chivalry
Return Type: String
Method Name: getOrder
Arg Types: (string, string)
Problem Statement

Problem Statement

Two lines (or queues) of people often have to merge into a single-file line. But, chivalry is not dead! When a man and a woman are about to merge, the man will always let the woman go first.

Given two lines of both men and women, write a method getOrder which will determine the genders of the people in the final line. If two women are at the front of both lines, the woman from the first line goes first. Likewise, if two men are at the front of both lines, the man from the first line goes first. Then, the people at the front of both lines are compared again.

Each input line will be a String of letters, each one representing either a man or a woman. Each man will be represented by an 'M' and each woman by a 'W'. The output should be of the same form. The front of each line is the left-most character of the String.

Constraints

  • first and second will each be between 1 and 50 characters long, inclusive.
  • first and second will consist of the characters 'M' and 'W' only.
Examples
0)
"M"
"W"
Returns: "WM"
1)
"MM"
"MW"
Returns: "MMMW"

Because of the 'tie breaker' rules, the first man from the first line goes first. Then, the second man from the first line goes, then the rest of the people (both from the second line.)

2)
"MMMM"
"W"
Returns: "WMMMM"
3)
"M"
"WWW"
Returns: "WWWM"
4)
"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMW"
"WMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
Returns: "WMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"

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

Coding Area

Language: C++17 · define a public class Chivalry with a public method string getOrder(string first, string second) · 27 test cases · 2 s / 256 MB per case

Submitting as anonymous