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
#include <bits/stdc++.h>

using namespace std;

const int N = 501;

char b[N][N];
int n, m, k, tp[269];

void mvup() {
    for (int j=1; j<=m; ++j) {
        int p=1;
        for (int i=1; i<=n; ++i) {
            char c=b[i][j];
            if (c == '.') continue;
            else b[i][j]='.', b[p++][j]=c;
        }
    }
}

void mvdw() {
    for (int j=1; j<=m; ++j) {
        int p=n;
        for (int i=n; i>=1; --i) {
            char c=b[i][j];
            if (c == '.') continue;
            else b[i][j]='.', b[p--][j]=c;
        }
    }
}

void mvlf() {
    for (int i=1; i<=n; ++i) {
        int p=1;
        for (int j=1; j<=m; ++j) {
            char c=b[i][j];
            if (c == '.') continue;
            else b[i][j]='.', b[i][p++]=c;
        }
    }
}

void mvrg() {
    for (int i=1; i<=n; ++i) {
        int p=m;
        for (int j=m; j>=1; --j) {
            char c=b[i][j];
            if (c == '.') continue;
            else b[i][j]='.', b[i][p--]=c; 
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>m;
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) cin>>b[i][j];
    }
    cin>>k;
    string s, cp; cin>>s;
    tp['L']=tp['P']=69, tp['D']=tp['G']=420;

    for (int i=0; i<k; ++i) {
        int j=i;
        for (j=i; j<k; ++j) {
            if (tp[s[i]] != tp[s[j]]) break;
        } --j;
        cp+=string(1, s[j]);

        i=j;
    }

    int sz=cp.size();
    for (int i=0; i<sz; ++i) {
        if (cp[i] == 'G') mvup();
        else if (cp[i] == 'D') mvdw();
        else if (cp[i] == 'L') mvlf();
        else if (cp[i] == 'P') mvrg();
        else assert(false);
    }

    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) cout<<b[i][j];
        cout<<"\n";
    }
}