--------
const int maxn=400; const int maxm=200000; int n,m; char a[maxn][maxn]; char b[maxn][maxn]; int stk[10]; int cnt; char s[maxm]; char cd[7][3]={"1","CX","V","BY","2","AZ","H"}; char sp[7][8]={"1","1H","1H1","1H1H","1H1H1","1H1H1H","H"}; void work(char cmd){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if (cmd=='1') b[j][i]=a[i][j]; if (cmd=='H') b[n-i+1][j]=a[i][j]; } } if (cmd=='1') swap(n,m); for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ a[i][j]=b[i][j]; } } } void stkPush(char cmd){ for (int i=0;i<7;i++){ if (strchr(cd[i],cmd)!=NULL){ int len=strlen(sp[i]); for (int j=0;j<len;j++){ if (cnt>0&&stk[cnt-1]==sp[i][j]) cnt--; else stk[cnt++]=sp[i][j]; if (cnt>=8) cnt=0; } break; } } } int main(){ while (~scanf("%d%d",&n,&m)){ cnt=0; for (int i=1;i<=n;i++) scanf("%s",a[i]+1); scanf("%s",s); int len=strlen(s); for (int i=0;i<len;i++) stkPush(s[i]); for (int i=0;i<cnt;i++) work(stk[i]); printf("%d %d ",n,m); for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ printf("%c",a[i][j]); } printf(" "); } } return 0; }
--------