Language: C++

#include <iostream>
#include <string>
#define ll long long
using namespace std;
const int M=1e6;
const int end=531441;
int s[M];
int b[1000];
void cha3(ll x,int n){
	for (int i=1;i<=n;i++)b[i]=0;
	int i=0;
	while (x){
		b[++i]=x%3;
		x/=3;
	}
}
void ops(int n){
	for (int i=1;i<=n;i++){
		if (b[i]==1)b[i]=2;
		else if (b[i]==2)b[i]=1;
	}
}
int pow[20];
void pre(){
	for (int i=0;i<end;i++){
		cha3(i,12);
		int ans=0,base=1;
		ops(12);
		for (int j=1;j<=12;j++){
			ans+=b[j]*base;
			base*=3;
		}
		s[i]=ans;
	}
	pow[1]=3,pow[2]=9,pow[3]=27,pow[4]=81,pow[5]=243,pow[6]=729;
	pow[7]=2187,pow[8]=6561,pow[9]=19683,pow[10]=59049;
	pow[11]=177147,pow[12]=531441;

}
int a[M];
int main(){
//		freopen("ab.txt","w",stdout);
	pre();
	int n;
	string s1;
	cin>>n>>s1;
	int cnt=0;
	for (int i=0;i<s1.size();i++){
		int ans=1;
		char c=s1[i];
		while (i+ans<s1.size() and s1[i+ans]==c)ans++;
		if (c=='S'){
			if (ans%2){
				a[++cnt]=M+1;
			}
		}else{
			a[++cnt]=ans;
		}
		i=i+ans-1;
	}
	for (int i=0;i<pow[n];i++){
		ll ans=i;
		for (int j=1;j<=cnt;j++){
			if (a[j]>M)ans=s[ans];
			else{
				ans+=a[j];
				if (ans>=pow[n])ans%=pow[n];
			}
		}
		printf("%lld ",ans);
	}
	return 0;
}



Compiler output:
a.cc:23:5: warning: built-in function 'pow' declared as non-function [-Wbuiltin-declaration-mismatch]
   23 | int pow[20];
      |     ^~~
a.cc: In function 'void pre()':
a.cc:25:24: error: reference to 'end' is ambiguous
   25 |         for (int i=0;i<end;i++){
      |                        ^~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
In file included from /usr/include/c++/14/bits/range_access.h:36:
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:6:11: note:                 'const int end'
    6 | const int end=531441;
      |           ^~~