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<bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 13;
const int maxp = 2e6 + 5;
const int maxm = 2e5 + 5;

int n, m;
int pw[maxn], ans[maxp];
char t[maxm];

class Trie{
public:
	int tot;
	int p[maxp], ch[maxp][3], tag[maxp];
	Trie(){
		tot = 1;
		memset(ch, -1, sizeof(ch));
		memset(p, -1, sizeof(p));
		return;
	}
	inline void insert(int x){
		int u = 0, y = x;
		for(int i = 0; i < n; ++i, x /= 3){
			if(!~ch[u][x % 3])
				ch[u][x % 3] = tot++;
			u = ch[u][x % 3];
		}
//		printf("u = %d y = %d\n", u, y);
		p[u] = y;
		return;
	}
	inline void inv(int u){
		tag[u] ^= 1;
		swap(ch[u][1], ch[u][2]);
		return;
	}
	inline void pushDown(int u){
		if(tag[u]){
			FOR(i, 0, 3)
				inv(ch[u][i]);
			tag[u] = false;
		}
		return;
	}
	inline void update(int dep, int u){
//		printf("dep = %d u = %d\n", dep, u);
		if(dep > n || !~u)
			return;
		pushDown(u);
		int tmp = ch[u][2];
		ch[u][2] = ch[u][1];
		ch[u][1] = ch[u][0];
		ch[u][0] = tmp;
		update(dep + 1, ch[u][0]);
		return;
	}
	inline void dfs(int dep, int u, int x){
		if(dep > n || !~u)
			return;
//		printf("dep = %d u = %d x = %d\n", dep, u, x);
		pushDown(u);
		FOR(i, 0, 3)
			dfs(dep + 1, ch[u][i], x + i * pw[dep]);
		if(~p[u])
			ans[p[u]] = x;
		return;
	}
}trie;

inline void precalc(){
	pw[0] = 1;
	FOR(i, 1, maxn)
		pw[i] = pw[i - 1] * 3;
	return;
}

int main(){
	scanf("%d", &n);
	precalc();
	scanf("%s", t);
	m = strlen(t);
	FOR(i, 0, pw[n])
		trie.insert(i);
	FOR(i, 0, m){
		if(t[i] == 'S')
			trie.inv(0);
		else
			trie.update(0, 0);
	}
	trie.dfs(0, 0, 0);
	FOR(i, 0, pw[n])
		printf("%d ", ans[i]);
	puts("");
	return 0;
}