#arc177b. B - Puzzle of Lamps

B - Puzzle of Lamps

Score: 400400 points

问题陈述

AtCoder先生创建了一个由NN个小灯泡组成的设备,这些灯泡从左到右排列成一行,并且有两个开关A和B。每个灯泡可以处于两种状态之一:0(关闭)和1(打开)。按下每个开关会导致以下情况:

  • 按下开关A将最左边的灯泡状态从0变为1
  • 按下开关B将最左边的灯泡状态从1变为0

如果没有适用的灯泡,则不能按下开关。

最初,所有灯泡都处于0状态。他希望灯泡的状态从左到右依次为S1,S2,,SNS_1, S_2, \dots, S_N。确定按下开关的顺序和次数,以实现这一点。没有必要最小化按下的次数,但应该最多为10610^6,以便操作可以在现实时间内完成。可以证明,在这个问题的约束条件下,存在解决方案。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Mr. AtCoder has created a device consisting of NN small light bulbs arranged in a row from left to right, and two switches A and B. Each light bulb can be in one of two states: 0 (OFF) and 1 (ON). Pressing each switch causes the following:

  • Pressing switch A turns the leftmost light bulb in the 0 state into 1.
  • Pressing switch B turns the leftmost light bulb in the 1 state into 0.

If there is no applicable light bulb, you cannot press the switch.

Initially, all light bulbs are in the 0 state. He wants the states of the light bulbs to be S1,S2,,SNS_1, S_2, \dots, S_N from left to right. Determine the order and number of times the switches should be pressed to achieve this. It is not necessary to minimize the number of presses, but it should be at most 10610^6 so that the operations can finish in a realistic time. It can be proved that a solution exists under the constraints of this problem.

Constraints

  • 1N301 \leq N \leq 30
  • Each of S1,S2,,SNS_1, S_2, \dots, S_N is 0 or 1.
  • Not all of S1,S2,,SNS_1, S_2, \dots, S_N are 0.
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

NN

S1S2SNS_1 S_2 \dots S_N

Note that the second line is given as a string of length NN.

Output

If your solution presses the switches mm times (1m106)(1 \leq m \leq 10^6) in the order t1,t2,,tmt_1, t_2, \dots, t_m (each being A or B), print those in the following format:

mm

t1t2tmt_1 t_2 \dots t_m

The second line should be printed as a string of length mm.

Sample Input 1

5
01100

Sample Output 1

4
AAAB

This sample output presents a solution that presses the switches in the order A, A, A, B. This sets the light bulbs to the desired states, as shown in the figure below:

Alternatively, pressing switches in the order A, A, B, A, A, B also sets the light bulbs to the desired states. The following output corresponding to this solution would also be accepted:

6
AABAAB