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.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Abc
{
    class QuestionC
    {
        public static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            // 整数の入力
            long n = long.Parse(Console.ReadLine());

            // 文字列の入力
            string s = Console.ReadLine();


            string[] a = new string[(int)Math.Pow(3,(double)n)];
            for (int i = 0; i < (int)Math.Pow(3, (double)n); i++)
            {
                a[i] = IntToN(i,3);
            }

            var result = new List<string>();
            foreach (var item in a) 
            {
                string str = item;
                foreach (char c in s)
                {
                    if (c == 'S')
                    {
                        string r = "";
                        foreach (var i in str)
                        {
                            if (i == '0')
                            {
                                r += "0";
                            }
                            else if (i == '1')
                            {
                                r += "2";
                            }
                            else
                            {
                                r += "1";
                            }
                        }
                        str = r.ToString();
                    }
                    else
                    {
                        str = IntToN(NToInt(str, 3) + 1 == (int)Math.Pow(3, (double)n) ? 0 : NToInt(str, 3) + 1, 3);
                    }
                }
                result.Add(str);
            }
            
            Console.WriteLine(result.JoinString(" ", x => NToInt(x, 3).ToString()));

            Console.Out.Flush();
        }
        /// <summary>
        /// int 型の値を N 進数を示す文字列に変換します。
        /// </summary>
        /// <param name="a"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        private static string IntToN(int a, int sp)
        {
            return ByteToN(BitConverter.GetBytes(a), sp);
        }

        /// <summary>
        /// バイト列を N 進数を示す文字列に変換します。
        /// </summary>
        /// <param name="b"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        private static string ByteToN(byte[] b, int sp)
        {
            long n = BitConverter.ToUInt32(b, 0);
            long num = n;

            char[] ch = getChar(sp);

            string result = string.Empty;

            long amari = 0;
            while (true)
            {
                if (num < sp)
                {
                    result = ch[num] + result;
                    break;
                }
                amari = num % sp;
                num = num / sp;
                result = ch[amari] + result;
            }
            return result;
        }

        /// <summary>
        /// N 進数を示す文字列を数字に変換します。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        private static int NToInt(string s, int sp)
        {
            return BitConverter.ToInt32(NToByte(s, sp), 0);
        }

        /// <summary>
        /// N 進数を示す文字列をバイト列に変換します。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        private static byte[] NToByte(string s, int sp)
        {
            s = s.ToLower();
            char[] ch = getChar(sp);

            long result = 0;
            int len = s.Length;

            int keta = 0;
            for (int i = len - 1; i >= 0; i--)
            {
                keta = len - i - 1;

                char target = s[i];
                int index = Array.IndexOf(ch, target);
                if (index < 0)
                {
                    throw new IndexOutOfRangeException();
                }
                result += index * ((long)Math.Pow(sp, keta));
            }

            uint are = (uint)(result & 0xFFFFFFFF);
            byte[] b = BitConverter.GetBytes(are);
            return b;
        }

        /// <summary>
        /// 文字テーブルを作成します。
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private static char[] getChar(int count)
        {
            char[] result = new char[count];
            for (int i = 0; i < 10 && i < count; i++)
            {
                result[i] = (char)('0' + ((char)i));
            }
            if (count > 10)
            {
                for (int i = 0; i < count - 10; i++)
                {
                    result[i + 10] = (char)('a' + ((char)i));
                }
            }
            return result;
        }
    }
    public static class IEnumerableUtil
    {
        public static string JoinString<T>(this IEnumerable<T> values, string separator, Func<T, string> converter = null)
        {
            if (converter != null)
            {
                List<string> buff = new List<string>();
                foreach (var item in values)
                    buff.Add(converter(item));
                return string.Join(separator, buff);
            }
            else
                return string.Join(separator, values);
        }
    }
}