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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define fi first
#define sc second
#define LL long long

vector<vector<char>> Tab;
vector<char> Moves;

void brute(int N, int M){
	for(int it = 0; it < Moves.size(); it++){
		vector<vector<char>> tTab;
		if(Moves[it] == 'L' || Moves[it] == 'P'){
			for(int i = 0; i < N; i++){
				deque<char> temp;
				for(int j = 0; j < M; j++){
					if(Tab[i][j] != '.') temp.push_back(Tab[i][j]);
				}
				if(Moves[it] == 'L'){
					while(temp.size() != M){
						temp.push_back('.');
					}
				}
				else{
					while(temp.size() != M){
						temp.push_front('.');
					}
				}
				vector<char> vtemp;
				while(!temp.empty()){
					vtemp.push_back(temp.front());
					temp.pop_front();
				}
				tTab.push_back(vtemp);
			} 
		}
		
		else{
			tTab.resize(N, vector<char>(M));
			for(int i = 0; i < M; i++){
				deque<char> temp;
				for(int j = 0; j < N; j++){
					if(Tab[j][i] != '.') temp.push_back(Tab[j][i]);
				}
				if(Moves[it] == 'G'){
					while(temp.size() != N) temp.push_back('.');
				}
				else{
					while(temp.size() != N) temp.push_front('.');
				}
				vector<char> vtemp;
				while(!temp.empty()){
					vtemp.push_back(temp.front());
					temp.pop_front();
				}
				for(int j = 0; j < N; j++){
					tTab[j][i] = vtemp[j];
				}
			}
		}
		Tab = tTab;
	}
	for(int it = 0; it < N; it++){
		for(int jt = 0; jt < M; jt++){
			cout << Tab[it][jt];
		}
		cout << endl;
	}
}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
	int N, M;
	cin >> N >> M;
	Tab.resize(N, vector<char>(M));
	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			cin >> Tab[i][j];
		}
	}
	int k; cin >> k;
	int StateLP = 0; int StateGD = 0;
	for(int i = 0; i < k; i++){
		char x;
		cin >> x;
		if(x == 'G'){
			if(StateGD == 0) Moves.push_back(x);
			else if(StateGD == 1){
				if(Moves[Moves.size() - 1] == 'D') Moves[Moves.size() - 1] = x;
				else Moves.push_back(x);
			}
			StateGD = -1;
		}
		else if(x == 'D'){
			if(StateGD == 0) Moves.push_back(x);
			else if(StateGD == -1){
				if(Moves[Moves.size() - 1] == 'G') Moves[Moves.size() - 1] = x;
				else Moves.push_back(x);
			}
			StateGD = 1;			
		}
		else if(x == 'L'){
			if(StateLP == 0) Moves.push_back(x);
			else if(StateLP == 1){
				if(Moves[Moves.size() - 1] == 'P') Moves[Moves.size() - 1] = x;
				else Moves.push_back(x);
			}
			StateLP = -1;			
		}
		else if(x == 'P'){
			if(StateLP == 0) Moves.push_back(x);
			else if(StateLP == -1){
				if(Moves[Moves.size() - 1] == 'L') Moves[Moves.size() - 1] = x;
				else Moves.push_back(x);
			}
			StateLP = 1;
		}
	}
	brute(N, M);
	return 0;       
}