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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Wiktor Kotala
#include <bits/stdc++.h>
#define f first
#define s second
#define add push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;
const int N = 503;

int n,m;
char P[N][N];
char p[N][N];
char K[N * 1000];
int x[N * 1000], y[N * 1000];

void brut(char kier){
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            p[i][j] = '.';

    if(kier == 'P'){
        for(int i=1; i<=n; i++){
            int k = m;
            for(int j=m; j>=1; j--){
                if(P[i][j] != '.'){
                    p[i][k--] = P[i][j];
                }
            }
        }
    }
    else if(kier == 'L'){
        for(int i=1; i<=n; i++){
            int k = 1;
            for(int j=1; j<=m; j++){
                if(P[i][j] != '.'){
                    p[i][k++] = P[i][j];
                }
            }
        }
    }
    else if(kier == 'G'){
        for(int j=1; j<=m; j++){
            int k = 1;
            for(int i=1; i<=n; i++){
                if(P[i][j] != '.'){
                    p[k++][j] = P[i][j];
                }
            }
        }
    }
    else{
        for(int j=1; j<=m; j++){
            int k = n;
            for(int i=n; i>=1; i--){
                if(P[i][j] != '.'){
                    p[k--][j] = P[i][j];
                }
            }
        }
    }

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            P[i][j] = p[i][j];
    return;
}

bool kierunek(char c){
    return (c=='G' || c=='D');
}

bool odp(bool koniec){
    if(koniec){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=m; j++){
                cout << P[i][j];
            }
            cout << '\n';
        }
        return 1;
    }
    return 0;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> n >> m;
	for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin >> P[i][j];
        }
	}

    int k;
    cin >> k;
    for(int i=1; i<=k; i++){
        cin >> K[i];
    }

    // do krawedzi
    int ind = 1;
    while(ind < k && kierunek(K[ind]) == kierunek(K[ind+1]))
        ind++;

    brut(K[ind]);
    if(K[ind]=='G') y[0] = 0;
    else if(K[ind]=='D') y[0] = 1;
    else if(K[ind]=='L') x[0] = 0;
    else x[0] = 1;

    if(odp(ind==k))
        return 0;

    // do rogu
    ind++;
    while(ind < k && kierunek(K[ind]) == kierunek(K[ind+1]))
        ind++;

    brut(K[ind]);
    if(K[ind]=='G') y[0] = 0;
    else if(K[ind]=='D') y[0] = 1;
    else if(K[ind]=='L') x[0] = 0;
    else x[0] = 1;

    if(odp(ind==k))
        return 0;

    // reszta
    while(ind < k){
        ind++;
        while(ind < k && kierunek(K[ind]) == kierunek(K[ind+1]))
            ind++;
        brut(K[ind]);
    }
    odp(1);

	return 0;
}