Connection Status:
Competition Arena > BinaryPad
SRM 116 · 2002-10-15 · by LunaticFringe
Class Name: BinaryPad
Return Type: String
Method Name: convert
Arg Types: (string)
Problem Statement

Problem Statement

You awake one morning from uneasy dreams to find that your best friend, Gregor, has transformed in his bed into a gigantic insect. By the frantic waving of his antennae, you deduce that he wants you to build a huge three-button pad in the shape of a circle. The buttons will each take up one third of the pad, and will be labeled A, B, and C. A is on the bottom, B on the upper left (clockwise from A), and C is on the upper right (counter-clockwise from A)

When Gregor wants to say something to you, he'll first convert the message to binary in his head. Then he'll climb onto the pad, starting at the A button. If the first digit in his binary message is 1, he would move clockwise onto button B. If the first digit is instead a 0, he would move counter-clockwise onto button C. He repeats in this fashion, moving clockwise (B, C, A, B, C, A, ...) for each 1 and counter-clockwise (C, B, A, C, B, A, ...) for each 0, until his message is finished.

Given a String presses, indicating which buttons Gregor pressed, return a String representing the binary message he communicated.

Notes

  • Gregor always starts on the A button. The first character of presses represents the button he moved to from A.

Constraints

  • presses will contain between 1 and 50 characters, inclusive.
  • presses will only contain the characters 'A', 'B', and 'C'.
  • No two adjacent characters of presses will be the same.
  • The first character of presses will be either 'B' or 'C'.
Examples
0)
"BCABC"
Returns: "11111"

Gregor moved clockwise five times (A->B, B->C, C->A, A->B, B->C). Therefore your function should return five 1's.

1)
"CBCABCB"
Returns: "0011110"

Two counterclockwise moves (A->C, C->B), four clockwise moves (B->C, C->A, A->B, B->C), and one more counterclockwise move (C->A) create the binary message "0011110".

2)
"BABABABABABABABABABABABABABABABABABABABABABABABABA"
Returns: "10101010101010101010101010101010101010101010101010"
3)
"C"
Returns: "0"
4)
"B"
Returns: "1"

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

Coding Area

Language: C++17 · define a public class BinaryPad with a public method string convert(string presses) · 24 test cases · 2 s / 256 MB per case

Submitting as anonymous