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
237
238
239
240
241
242
243
244
245
246
247
248
249
// Marcin Knapik

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;}
template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;}

template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;}

int n, m;

using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;

const int N = 505;
string orig[N];
using mac = vector<vector<ii>>;

ii operator + (ii a, ii b){
    return {a.f + b.f, a.s + b.s};
}

ii operator - (ii a, ii b){
    return {a.f - b.f, a.s - b.s};
}

map<char, ii> mapa_kier = {
    {'L', {0, -1}},
    {'P', {0, 1}},
    {'G', {-1, 0}},
    {'D', {1, 0}}
};

bool kolor(ii poz){
    return orig[poz.f][poz.s] != '.';
}

bool bounds(ii poz){
    return poz.f >= 0 and poz.s >= 0 and poz.f < n and poz.s < m;
}

mac make_move(mac mapa, char c){
    ii kier = mapa_kier[c];

    vii startowe;
    if(c == 'L' or c == 'P'){
        for(int i = 0; i < n; i++) {
            startowe.pb({i, (m - 1) * (c == 'L')});
        }
    } else {
        for(int j = 0; j < m; j++) {
            startowe.pb({(n - 1) * (c == 'G'), j});
        }
    }

    mac ret = mac(sz(mapa), vii(sz(mapa[0])));
    for(auto start : startowe){
        vii pelne;
        vii puste;

        while(bounds(start)){
            if(kolor(mapa[start.f][start.s])){
                pelne.pb(mapa[start.f][start.s]);
            } else {
                puste.pb(mapa[start.f][start.s]);
            }
            start = start + kier;
        }

        start = start - kier;
        while(bounds(start)){
            if(sz(pelne)){
                mapa[start.f][start.s] = pelne.back();
                pelne.pop_back();
            } else {
                mapa[start.f][start.s] = puste.back();
                puste.pop_back();
            }
            start = start - kier;
        }
        assert(sz(pelne) == 0 and sz(puste) == 0);
    }

    return mapa;
}

mac operator * (mac a, mac b){
    assert(sz(a) == sz(b) and sz(a) > 0 and sz(a[0]) == sz(b[0]));
    mac ret = a;
    for(int i = 0; i < sz(ret); i++){
        for(int j = 0; j < sz(ret[0]); j++){
            ii step = a[i][j];
            ret[i][j] = b[step.f][step.s];
        }
    }
    return ret;
}

mac operator / (mac a, mac b){
    assert(sz(a) == sz(b) and sz(a) > 0 and sz(a[0]) == sz(b[0]));
    mac ret = a;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            ret[b[i][j].f][b[i][j].s] = a[i][j];
        }
    }
    return ret;
}

mac id(){
    mac ret(n, vii(m));
    FOR(i, n){
        FOR(j, m){
            ret[i][j] = {i, j};
        }
    }
    return ret;
}

mac operator ^ (mac a, int pot){
    mac ret = id(); 
    while(pot){
        if(pot & 1){
            ret = ret * a;
        }
        a = a * a;
        pot >>= 1;
    }
    return ret;
}

void wypisz(mac mapa){
    FOR(i, n){
        FOR(j, m){
            cout << orig[mapa[i][j].f][mapa[i][j].s];
        }
        cout << '\n';
    }
}

void debug(mac mapa){
    FOR(i, n){
        FOR(j, m){
            cout << "(" << mapa[i][j].f << ',' << mapa[i][j].s << ")" << ' ';
        }
        cout << '\n';
    }
}

void solve () {
    cin >> n >> m;

    FOR(i, n){
        cin >> orig[i];
    }

    vector<vector<ii> > mapa = id();

    string ruchy;
    cin >> ruchy >> ruchy;

    auto reduce = [](string s){
        map<char, char> inny = {{'G', 'D'}, {'D', 'G'}, {'L', 'P'}, {'P', 'L'}};
        string ret;
        for(auto & u : s){
            if(sz(ret) and (ret.back() == inny[u] or ret.back() == u)) {
                ret.pop_back();
            }
            if(sz(ret) <= 1 or ret[sz(ret) - 2] == inny[u]){
                ret += u;
            }
        }
        return ret;
    };

    ruchy = reduce(ruchy);

    auto zmiana = [](mac pocz, mac kon){
        mac ret(n, vii(m));
        map<ii, ii> where;
        
        FOR(i, n){
            FOR(j, m){
                where[kon[i][j]] = {i, j};
            }
        }
        
        FOR(i, n){
            FOR(j, m){
                ret[i][j] = where[pocz[i][j]];
            }
        }

        return ret;
    };

    if(sz(ruchy) < 6){
        // int cnt = 0;
        for(auto & u : ruchy){
            // cout << cnt++ << endl;
            // wypisz(mapa);
            mapa = make_move(mapa, u);
        }
    } else {
        mapa = make_move(mapa, ruchy[0]);
        mapa = make_move(mapa, ruchy[1]);

        auto pom = mapa;
        for(int i = 2; i < 6; i++){
            pom = make_move(pom, ruchy[i]);
        } 
        int ile = (sz(ruchy) - 2) / 4;

        mapa = mapa / (zmiana(mapa, pom) ^ ile);
        for(int i = 2 + ile * 4; i < sz(ruchy); i++){
            mapa = make_move(mapa, ruchy[i]);
        }
    }

    wypisz(mapa);
}

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

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++) {
        solve();
    }

    return 0;
}