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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.C
{
    public class Program
    {
        public static long N { get; private set; }
        public static int Max { get; private set; }
        public static Dictionary<long, long> Sarusa { get; private set; }

        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            N = ReadLong();
            var T = ReadText().Replace("SS", "");

            Max = (int)Math.Pow(3, N);
            var P = Enumerable.Range(0, Max).Select(x => (long)x).ToList();

            CreateSarusaMap();

            for (int i = 0; i < T.Length; i++)
            {
                var dance = T[i];
                switch (dance)
                {
                    case 'S':
                        S(P);
                        break;
                    case 'R':
                        R(P);
                        break;
                }
            }

            return string.Join(" ", P);
        }

        private static void CreateSarusaMap()
        {
            var sansin = new Queue<string>();
            sansin.Enqueue("0");
            sansin.Enqueue("1");
            sansin.Enqueue("2");

            for (var i = 0; i < 11; i++)
            {
                var count = sansin.Count;
                for (var j = 0; j < count; j++)
                {
                    var x = sansin.Dequeue();
                    sansin.Enqueue(x + "0");
                    sansin.Enqueue(x + "1");
                    sansin.Enqueue(x + "2");
                }
            }

            Sarusa = new Dictionary<long, long>() { };
            while (sansin.Count > 0)
            {
                var x = sansin.Dequeue();
                var key = 0L;
                var val = 0L;
                for (var i = 0; i < 12; i++)
                {
                    var s = long.Parse(x[i].ToString());
                    key += (long)Math.Pow(3, i) * s;
                    val += (long)Math.Pow(3, i) * (s == 1 ? 2 : s == 2 ? 1 : 0);
                }

                Sarusa.Add(key, val);
            }
        }

        private static void S(List<long> p)
        {
            for (var i = 0; i < Max; i++)
            {
                p[i] = Sarusa[p[i]];
            }
        }
        private static void R(List<long> p)
        {
            for (var i = 0; i < Max; i++)
            {
                p[i]++;
                if (p[i] == Max)
                {
                    p[i] = 0;
                }
            }
        }
        private static void R(List<long> p, long count)
        {
            for (var i = 0; i < Max; i++)
            {
                p[i] += count;
                if (p[i] >= Max)
                {
                    p[i] -= Max;
                }
            }
        }

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

    }
}