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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); }

#ifdef LOCAL
#define _upgrade ios_base::sync_with_stdio(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DBG(...) (__VA_ARGS__)
#define cerr if(0) cout
#endif

// ********************** CODE ********************** //

const int N = 505;
const int K = 5e5 + 7;

int n, m, k, T[N][N], memo[N][N], P[N * N], PM[N * N];
char S[N][N], mp[N * N];

string buf, tape;

bool opp(char x, char y)
{
    if (x == 'G' && y == 'D')
        return true;
    if (x == 'D' && y == 'G')
        return true;
    if (x == 'L' && y == 'P')
        return true;
    if (x == 'P' && y == 'L')
        return true;
    return false;
}

void sim(char c)
{
    if (c == 'G')
    {
        for (int j = 1; j <= m; j++)
        {
            int it = 1;
            for (int i = 1; i <= n; i++)
            {
                if (T[i][j] != 0)
                {
                    T[it][j] = T[i][j];
                    it++;
                }
            }
            while (it <= n)
            {
                T[it][j] = 0;
                it++;
            }
        }
    }
    if (c == 'D')
    {
        for (int j = 1; j <= m; j++)
        {
            int it = n;
            for (int i = n; i >= 1; i--)
            {
                if (T[i][j] != 0)
                {
                    T[it][j] = T[i][j];
                    it--;
                }
            }
            while (it >= 1)
            {
                T[it][j] = 0;
                it--;
            }
        }
    }
    if (c == 'L')
    {
        for (int i = 1; i <= n; i++)
        {
            int it = 1;
            for (int j = 1; j <= m; j++)
            {
                if (T[i][j] != 0)
                {
                    T[i][it] = T[i][j];
                    it++;
                }
            }
            while (it <= m)
            {
                T[i][it] = 0;
                it++;
            }
        }
    }
    if (c == 'P')
    {
        for (int i = 1; i <= n; i++)
        {
            int it = m;
            for (int j = m; j >= 1; j--)
            {
                if (T[i][j] != 0)
                {
                    T[i][it] = T[i][j];
                    it--;
                }
            }
            while (it >= 1)
            {
                T[i][it] = 0;
                it--;
            }
        }
    }
}

void print()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << (T[i][j] ? mp[T[i][j]] : '.');
        cout << "\n";
    }
}

int cnt = 0;
bool vis[N * N];

void move_P(int mvs)
{
    for (int i = 1; i <= cnt; i++)
    {
        if (vis[i])
            continue;

        vector<int> cycle;
        int cur = i;
        while (!vis[cur])
        {
            cycle.push_back(cur);
            vis[cur] = true;
            cur = P[cur];
        }
        for (int j = 0; j < sz(cycle); j++)
            PM[cycle[j]] = cycle[(j + mvs) % sz(cycle)];
    }
}

int main()
{
    _upgrade
    
    cin >> n >> m;

    for (int i = 1; i <= n; i++)
    {
        cin >> (S[i] + 1);
        for (int j = 1; j <= m; j++)
        {
            if (S[i][j] != '.')
            {
                T[i][j] = ++cnt;
                mp[cnt] = S[i][j];
            }
        }
    }

    cin >> k >> buf;
    tape.push_back(buf[0]);
    for (int i = 1; i < k; i++)
    {
        char c = buf[i];
        if (c == tape.back())
            continue;
        if (opp(c, tape.back()))
        {
            tape.pop_back();
            if (sz(tape) < 2)
                tape.push_back(c);
            continue;
        }
        if (sz(tape) < 2 || opp(tape[sz(tape) - 2], c))
            tape.push_back(c);
    }

    int f = sz(tape) % 4;
    if (f < 2)
        f += 4;


    for (int i = 0; i < min(f, sz(tape)); i++)
        sim(tape[i]);

    if (sz(tape) > f)
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                memo[i][j] = T[i][j];
        for (int i = f; i < f + 4; i++)
            sim(tape[i]);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                assert((memo[i][j] == 0 && T[i][j] == 0) || (memo[i][j] && T[i][j]));
                if (T[i][j])
                {
                    P[memo[i][j]] = T[i][j];
                }
            }
        }
        int mvs = (sz(tape) - f) / 4;
        move_P(mvs);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                T[i][j] = PM[memo[i][j]];
    }

    print();
    return 0;
}