1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include<cstdio>
const int N=505,M=500005,K=N*N;
int n,m,cnt,q,i,j,cp,loop,id[N][N],pre[N][N],g[K],h[K],cir[K],len;
char a[N][N],op[M],pool[M],col[K];
bool vis[K];
inline void append(char x){
	while(cp){
		char y=pool[cp];
		if((y=='L'||y=='R')==(x=='L'||x=='R')){
			cp--;
			continue;
		}
		if(cp>1){
			y=pool[cp-1];
			if(y==x)return;
		}
		break;
	}
	pool[++cp]=x;
}
inline void simulate(char dir){
	int i,j,x;
	static int have[N],other[N],cur[N];
	if(dir=='L'||dir=='R'){
		for(i=1;i<=n;i++){
			int chave=0,cother=0,ccur=0;
			for(j=1;j<=m;j++){
				x=id[i][j];
				if(col[x]!='.')have[++chave]=x;
				else other[++cother]=x;
			}
			if(dir=='L'){
				for(j=1;j<=chave;j++)cur[++ccur]=have[j];
				for(j=1;j<=cother;j++)cur[++ccur]=other[j];
			}else{
				for(j=1;j<=cother;j++)cur[++ccur]=other[j];
				for(j=1;j<=chave;j++)cur[++ccur]=have[j];
			}
			for(j=1;j<=m;j++)id[i][j]=cur[j];
		}
	}else{
		for(i=1;i<=m;i++){
			int chave=0,cother=0,ccur=0;
			for(j=1;j<=n;j++){
				x=id[j][i];
				if(col[x]!='.')have[++chave]=x;
				else other[++cother]=x;
			}
			if(dir=='U'){
				for(j=1;j<=chave;j++)cur[++ccur]=have[j];
				for(j=1;j<=cother;j++)cur[++ccur]=other[j];
			}else{
				for(j=1;j<=cother;j++)cur[++ccur]=other[j];
				for(j=1;j<=chave;j++)cur[++ccur]=have[j];
			}
			for(j=1;j<=n;j++)id[j][i]=cur[j];
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		scanf("%s",a[i]+1);
		for(j=1;j<=m;j++){
			id[i][j]=++cnt;
			col[cnt]=a[i][j];
		}
	}
	scanf("%d%s",&q,op+1);
	for(i=1;i<=q;i++){
		if(op[i]=='G')op[i]='U';
		if(op[i]=='P')op[i]='R';
		append(op[i]);
	}
	//for(i=1;i<=cp;i++)putchar(pool[i]);puts("");
	for(i=1;i<=cp&&i<=4;i++)simulate(pool[i]);
	for(i=5;i+3<=cp;i+=4)loop++;
	int _=i;
	if(loop){
		for(i=1;i<=n;i++)for(j=1;j<=m;j++)pre[i][j]=id[i][j];
		for(i=1;i<=4;i++)simulate(pool[i]);
		for(i=1;i<=n;i++)for(j=1;j<=m;j++)g[pre[i][j]]=id[i][j];
		for(i=1;i<=cnt;i++)if(!vis[i]){
			len=0;
			for(j=i;!vis[j];j=g[j]){
				cir[len++]=j;
				vis[j]=1;
			}
			for(j=0;j<len;j++)h[cir[j]]=cir[(j+loop-1)%len];
		}
		for(i=1;i<=n;i++)for(j=1;j<=m;j++)id[i][j]=h[id[i][j]];
	}
	for(i=_;i<=cp;i++)simulate(pool[i]);
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++)putchar(col[id[i][j]]);
		puts("");
	}
}
/*
GDLP
UDLR
*/