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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <bits/stdc++.h>
using namespace std;

void cvrg(vector<vector<char>> &w)
{
    int n = w[0].size(), m = w.size(), p = 0;
    char t;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (w[j][i] != '.')
            {
                t = w[j][i];
                w[j][i] = '.';
                w[p][i] = t;
                p++;
            }
        }

        p = 0;
    }
}

void cvrd(vector<vector<char>> &w)
{
    int n = w[0].size(), m = w.size(), p = m - 1;
    char t;

    for (int i = 0; i < n; i++)
    {
        for (int j = m - 1; j >= 0; j--)
        {
            if (w[j][i] != '.')
            {
                t = w[j][i];
                w[j][i] = '.';
                w[p][i] = t;
                p--;
            }
        }

        p = m - 1;
    }
}

void cvrl(vector<vector<char>> &w)
{
    int n = w.size(), m = w[0].size(), p = 0;
    char t;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (w[i][j] != '.')
            {
                t = w[i][j];
                w[i][j] = '.';
                w[i][p] = t;
                p++;
            }
        }

        p = 0;
    }
}

void cvrp(vector<vector<char>> &w)
{
    int n = w.size(), m = w[0].size(), p = m - 1;
    char t;

    for (int i = 0; i < n; i++)
    {
        for (int j = m - 1; j >= 0; j--)
        {
            if (w[i][j] != '.')
            {
                t = w[i][j];
                w[i][j] = '.';
                w[i][p] = t;
                p--;
            }
        }

        p = m - 1;
    }
}

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

    int n, m, k;
    cin >> n >> m;

    string s;
    vector<vector<char>> w(n, vector<char>(m, '.'));

    for (int i = 0; i < n; i++)
    {
        cin >> s;

        for (int j = 0; j < m; j++)
        {
            w[i][j] = s[j];
        }
    }

    cin >> k;// >> s;

    char p, pp = '.';
    
    for (int i = 0; i < k; i++)
    {
        //scanf("%c", &p);
        cin >> p;
        //cout << i << " ";

        if (p != pp)
        {
            if (p == 'P')
            {
                cvrp(w);
            }
            else if (p == 'L')
            {
                cvrl(w);
            }
            else if (p == 'G')
            {
                cvrg(w);
            }
            else
            {
                cvrd(w);
            }
        }
        
        pp = p;
    }

    for (int x = 0; x < n; x++)
    {
        for (int y = 0; y < m; y++)
        {
            cout << w[x][y];
        }

        cout << "\n";
    }
}