DyckwordUniformer
SRM 274 · 2005-11-23 · by Andrew_Lazarev
Problem Statement
A Dyck word is a string consisting of n X's and n Y's (for some positive integer n) such that no initial segment of the string has more Y's than X's.
If we have a Dyck word we can swap any two consecutive substrings when both of them are Dyck words. It is easy to show that the result of this swap operation is a Dyck word too. For example, in "XXYXXYYY" (quotes for clarity) we can swap the substrings "XY" and "XXYY" to get "XXXYYXYY".
If a Dyck word A can be obtained from a Dyck word B using some number of the described swap operations they are called equivalent.
You will be given a
Constraints
- dyckword will contain between 2 and 50 characters, inclusive.
- Each character in dyckword will be either 'X' or 'Y'.
"XXYXXYYY" Returns: "XXXYYXYY"
The example from the problem statement.
"XYXYXXXYYYXXYY" Returns: "XXXYYYXXYYXYXY"
The result can be obtained by four swaps "XYXYXXXYYYXXYY" -> "XYXXXYYYXYXXYY" -> "XXXYYYXYXYXXYY" -> "XXXYYYXYXXYYXY" -> "XXXYYYXXYYXYXY".
"XXXYYYXXYXXXYYYY" Returns: "XXXXYYYXYYXXXYYY"
The result can be obtained by two swaps "XXXYYYXXYXXXYYYY" -> "XXXYYYXXXXYYYXYY" -> "XXXXYYYXYYXXXYYY".
"XXYYYX" Returns: ""
"XXXXXXXXXXXXXXXXXXXXXXXXXYYYYYYYYYYYYYYYYYYYYYYYYY" Returns: "XXXXXXXXXXXXXXXXXXXXXXXXXYYYYYYYYYYYYYYYYYYYYYYYYY"
Submissions are judged against all 86 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class DyckwordUniformer with a public method string uniform(string dyckword) · 86 test cases · 2 s / 256 MB per case