Problem:
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".

After the first salsa, the positions are: 0, 2, 1.

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).

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

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <bitset>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
const int MAX_N = 540010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N;
string S;
int A[MAX_N];

bitset<540000> B[6];
vi V[6] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
int to[6] = {4, 2, 5, 0, 3, 1};
int I[6], rI[6];
int Q[15][MAX_N];
int ans[MAX_N];
int inv[MAX_N];

int find_idx(int a) {
	rep(i, 0, 6) {
		if(B[i][a]) return i;
	}
	return -1;
}

void idx_move(int x, int y, int a) {
	B[x][a] = 0;
	B[y][a] = 1;
};

void solve() {
	cin >> N;
	cin >> S;
	// N = 12;
	// rep(i, 0, 200000) {
	// 	int v = rand() % 2;
	// 	if(v == 0) S += 'R';
	// 	else S += 'S';
	// }
	int T = sz(S);
	
	int BN = 1;
	rep(i, 0, N) BN *= 3;
	rep(q, 0, N) {
		int pow3 = 1;
		rep(i, 0, q) pow3 *= 3;
		rep(i, 0, 6) {
			B[i].reset();
			I[i] = i;
			rI[i] = i;
		}
		rep(i, 0, pow3) B[0][i] = 1;

		rep(i, 0, T) {
			if(S[i] == 'R') {
				int v = Q[q][i];
				int idx = find_idx(v);
				int ridx = I[idx];
				int c = V[ridx][2];
				idx_move(idx, rI[to[ridx]], v);
				Q[q + 1][i] = c * pow3 + v;
			}
			else {
				swap(rI[0], rI[1]);
				swap(rI[2], rI[3]);
				swap(rI[4], rI[5]);
				rep(i, 0, 6) {
					I[rI[i]] = i;
				}
			}
		}
		rep(i, 0, pow3) {
			int v = ans[i];
			int idx = find_idx(v);
			int ridx = I[idx];
			rep(j, 0, 3) {
				ans[j * pow3 + i] = V[ridx][j] * pow3 + v;
			}
		}
	}
	rep(i, 0, BN) {
		inv[ans[i]] = i;
	}
	rep(i, 0, BN) {
		cout << inv[i] << " ";
	}
	cout << "\n";
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
	solve();
	return 0;
}