Connection Status:
Competition Arena > AlphaWord
SRM 124 · 2002-12-12 · by dgoodman
Class Name: AlphaWord
Return Type: String
Method Name: firstAlpha
Arg Types: (string)
Problem Statement

Problem Statement

The government of a small but important country has decided that the alphabet needs to be streamlined and reordered. Uppercase letters will be eliminated. They will issue a decree in the form of a string of 'B' and 'A' characters corresponding to Before or After. The first character in the decree specifies whether 'a' must come Before 'b' in the new alphabet or After 'b'. The second character determines the relative placement of 'b' and 'c', and in general, character i of the decree specifies the relative placement in the new alphabet of characters i and i+1 from the old alphabet. So, for example, "BAAB" means that 'a' must come Before 'b', 'b' must come After 'c', 'c' must come After 'd', and 'd' must come Before 'e'.

Any letters beyond these requirements are to be excluded, so if the decree specifies k comparisons then the new alphabet will contain the first k+1 lowercase letters of the current alphabet.

Create a class Alphabet that contains the method firstAlpha that takes the decree as input and returns the new alphabet as a String that is first alphabetically among all alphabets that conform to the decree. Specifically, if 2 alphabets conform to the decree, choose the one with the lower character in the first position in which they differ.

Constraints

  • decree contains between 1 and 25 characters, inclusive.
  • Each character in decree is the uppercase letter 'B' or 'A'.
Examples
0)
"BAA"
Returns: "adcb"

'a' Before 'b', 'b' After 'c', and 'c' After 'd' have been decreed, so possibilities are "adcb", "dacb", "dcab".

1)
"AAAA"
Returns: "edcba"

"edcba" is the only alphabet that conforms.

2)
"BBAA"
Returns: "abedc"

all the alphabets that satisfy this are: "edabc", "eadbc", "aedbc", "eabdc", "aebdc", and "abedc". Of these, the first is "abedc".

3)
"BABAAA"
Returns: "acbgfed"
4)
"A"
Returns: "ba"

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

Coding Area

Language: C++17 · define a public class AlphaWord with a public method string firstAlpha(string decree) · 25 test cases · 2 s / 256 MB per case

Submitting as anonymous