|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
char s[27];
char a[110],b[110],c[110],d[110];
char s1[30010],ss[30010];
int la,lb,lc,ll;
int lr=1;
int minl=2147483647;
inline void add(char a,char b,char c)
{
ss[ll++]=a;
ss[ll++]=' ';
ss[ll++]=b;
ss[ll++]=' ';
ss[ll++]=c;
ss[ll++]='\n';
}
void dfs()
{
if( ll > minl ) return;
if( lr == n+1 )
{
ss[ll]=0;
if( ll < minl || ll == minl && strcmp(ss,s1) == -1 )
{
minl=ll;
strcpy(s1,ss);
}
return;
}
char xx;
if( la )
{
xx=a[la-1];
la--;
if( xx == d[n-lr] )
{
lr++;
add(xx,'A','D');
dfs();
lr--;
ll-=6;
}
else
{
b[lb++]=xx;
add(xx,'A','B');
dfs();
lb--;
ll-=6;
c[lc++]=xx;
add(xx,'A','C');
dfs();
lc--;
ll-=6;
}
a[la++]=xx;
}
if( lb )
{
xx=b[lb-1];
lb--;
if( xx == d[n-lr] )
{
lr++;
add(xx,'B','D');
dfs();
lr--;
ll-=6;
}
else
{
c[lc++]=xx;
add(xx,'B','C');
dfs();
lc--;
ll-=6;
}
b[lb++]=xx;
}
if( lc )
{
xx=c[lc-1];
if( xx == d[n-lr] )
{
lr++;
lc--;
add(xx,'C','D');
dfs();
c[lc++]=xx;
lr--;
ll-=6;
}
}
}
int main()
{
scanf("%d%s",&n,s);
for(int i=0;i<n;i++)
{
a[i]=i+'a';
d[i]=s[i];
}
la=n;
dfs();
printf("%s",s1[0]?s1:"NO");
return 0;
} |
|