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: Wrong Answer)

#include<stdio.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline char CH()
{
  static char buf[100000],*ptr1=buf,*ptr2=buf;
  return ptr1==ptr2&&(ptr2=(ptr1=buf)+fread(buf,1,100000,stdin),ptr1==ptr2)?EOF:*ptr1++;
}
static inline int IN(void)
{
  int x=0,f=0,c=CH();while(c<48||c>57){f^=c==45,c=CH();}
  while(c>47&&c<58){x=x*10+c-48,c=CH();}return f?-x:x;
}
static inline void OUT(int x){if(x>=10)OUT(x/10);printf("%d",x%10);}
static inline int Pow(int a,int b){return b?Pow(a*a,b>>1)*(b&1?a:1):1;}
int main()
{
  int N=IN(),now,len=0,pw3=Pow(3,N),x=Pow(3,N>>1),y=Pow(3,N-(N>>1)),A[531441],B[531441];
  char c=CH(),T[200001],S[200000];
  rep(i,pw3)B[i]=B[i/3]*3+(3-i%3)%3;
  while(c!=EOF){if(c=='\t'||c==' '||c=='\n'){c=CH();}T[len++]=c,c=CH();}
  fflush(stdin);
  rep(i,x)
  {
    int temp=i;now=0;
    rep(j,len)
    {
      if(T[j]=='S'){temp=B[temp];if(S[now]=='S')--now;else S[++now]='S';}
      else{++temp;if(temp==x)S[++now]='R',temp=0;}
    }
    rep(j,y)
    {
      int keep=j;
      rep(k,now)
      {
        if(S[k+1]=='S')keep=B[keep];
        else if(S[k+1]=='R')if((++keep)==y)keep=0;
      }
      A[i+j*x]=temp+keep*x;
    }
  }
  rep(i,pw3)OUT(A[i]),putchar(' ');
  return fflush(stdout),0;
}