C - Strange Dance 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 1000

問題文

3^N 人の人が輪になって踊っています。 輪の中の人がいる位置に、0,1,\dots, 3^{N}-1 の番号を、適当な場所から始めて時計回りに付けます。はじめ、これらの位置それぞれに一人の人が立っています。

これから二種類の曲、サルサとルンバが流れ、人々はそれに合わせて踊ります。

  • サルサが流れたら、位置 i にいる人は以下で述べるような位置 j に移動します。j は、i3 進法で表記し、1 という桁をそれぞれ 2 に、2 という桁をそれぞれ 1 に置き換えて得られる数です (例えば、位置 46 の人は位置 65 に移動します)。
  • ルンバが流れたら、位置 i にいる人は位置 i+1 に移動します。ここで、位置 3^N は位置 0 とみなします。

文字列 T=T_1T_2\cdots T_{|T|} が与えられます。これは、T_i=S なら i 番目に流れる曲がサルサであり、T_i=R ならルンバであることを表します。 はじめ位置 i に立っていた人が、すべての曲が流れたあとに位置 P_i に立っているとします。 列 P_0,P_1,\dots, P_{3^N-1} を求めてください。

制約

  • 1 \le N \le 12
  • 1 \le |T| \le 200,000
  • TSR からなる。

入力

入力は標準入力から以下の形式で与えられる。

N
T

出力

以下の形式で標準出力に出力せよ。

P_0 P_1 \cdots P_{3^N-1}

入力例 1

1
SRS

出力例 1

2 0 1 

最初の曲が流れる前に位置 i に立っていた人を人 i とします。

  1. サルサが一度目に流れたあと、人 0, 1, 2 はそれぞれ位置 0, 2, 1 に立っています。
  2. ルンバが流れたあと、人 0, 1, 2 はそれぞれ位置 1, 0, 2 に立っています。
  3. サルサが二度目に流れたあと、人 0, 1, 2 はそれぞれ位置 2, 0, 1 に立っています。

入力例 2

2
RRSRSSSSR

出力例 2

3 8 1 0 5 7 6 2 4 

入力例 3

3
SRSRRSRRRSRRRR

出力例 3

23 9 22 8 3 7 20 24 19 5 18 4 17 12 16 2 6 1 14 0 13 26 21 25 11 15 10 

Score : 1000 points

Problem Statement

There are 3^N people dancing in circle. We denote with 0,1,\dots, 3^{N}-1 the positions in the circle, starting from an arbitrary position and going around clockwise. Initially each position in the circle is occupied by one person.

The people are going to dance on two kinds of songs: salsa and rumba.

  • When a salsa is played, the person in position i goes to position j, where j is the number obtained replacing all digits 1 with 2 and all digits 2 with 1 when reading i in base 3 (e.g., the person in position 46 goes to position 65).
  • When a rumba is played, the person in position i moves to position i+1 (with the identification 3^N = 0).

You are given a string T=T_1T_2\cdots T_{|T|} such that T_i=S if the i-th song is a salsa and T_i=R if it is a rumba. After all the songs have been played, the person that initially was in position i is in position P_i. Compute the array P_0,P_1,\dots, P_{3^N-1}.

Constraints

  • 1 \le N \le 12
  • 1 \le |T| \le 200,000
  • T contains only the characters S and R.

Input

Input is given from Standard Input in the following format:

N
T

Output

You should print on Standard Output:

P_0 P_1 \cdots P_{3^N-1}

Sample Input 1

1
SRS

Sample Output 1

2 0 1 

Before any song is played, the positions are: 0, 1, 2.

When we say "person i", we mean "the person that was initially in position i".

  1. After the first salsa, the positions are: 0, 2, 1.
  2. After the rumba, the positions are: 1, 0, 2 (so, person 0 is in position 1, person 1 is in position 0 and person 2 is in position 2).
  3. After the second salsa, the positions are 2, 0, 1 (so, person 0 is in position 2, person 1 is in position 0 and person 2 is in position 1).

Sample Input 2

2
RRSRSSSSR

Sample Output 2

3 8 1 0 5 7 6 2 4 

Sample Input 3

3
SRSRRSRRRSRRRR

Sample Output 3

23 9 22 8 3 7 20 24 19 5 18 4 17 12 16 2 6 1 14 0 13 26 21 25 11 15 10