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
//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < k; ++i)
#define per(i, p, k) for(int i(p); i > k; --i)
#define sz(x) (int)x.size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fir first
#define sec second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
    string name{""};
    time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
    duration<float, std::milli> dur;
    Timer() = default;
    Timer(string name): name(name) {}
    ~Timer() {
        end = high_resolution_clock::now(); dur= end - start;
        cout << "@" << name << "> " << dur.count() << " ms" << '\n';
    }
};
template <typename T = int> inline T in()
{
    T x;
    std::cin >> x;
    return x;
}
std::string yn(bool b)
{
    if(b) return "YES\n";
    else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
    for(const auto& i : wek)out << i << ' ';
    return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
    out << '{'<<par.first<<", "<<par.second<<"}";
    return out;
}
#define show(x) cout << #x << " = " << x << '\n';
constexpr int maxn = 503;
array <array <char, maxn>, maxn> tab;
int ruch(char c)
{
    if(c == 'G')return 1;
    if(c == 'D')return -1;
    if(c == 'P')return 2;
    if(c == 'L')return -2;
    exit(4);
}
template <typename T>
const T zer = {};
template <> const char zer<char> = '.';
template <> const pair <int, int> zer<pair <int, int>> = {-1, -1};
template <typename T = char>
void exec(int op, int n, int m, array<array <T, maxn>, maxn>& co = tab)
{
    // cerr << "exec("<<op<<")\n";
    if(abs(op) == 1)
    {
        rep(i, 0, m){
            vector <T> v;
            rep(j, 0, n)if(co[j][i] != zer<T>)v.pb(co[j][i]);
            rep(j, 0, n)co[j][i] = zer<T>;
            if(op < 0)reverse(all(v));
            rep(j, 0, sz(v)){
                if(op > 0)co[j][i] = v[j];
                else  co[n-1-j][i] = v[j];
            }
        }
    }
    if(abs(op) == 2)
    {
        rep(i, 0, n){
            vector <T> v;
            rep(j, 0, m)if(co[i][j] != zer<T>)v.pb(co[i][j]);
            rep(j, 0, m)co[i][j] = zer<T>;
            if(op > 0)reverse(all(v));
            rep(j, 0, sz(v)){
                if(op < 0)co[i][j] = v[j];
                else  co[i][m-1-j] = v[j];
            }
        }
    }
}
using Arr = array <array <pair <int, int>, maxn>, maxn>;
map <int, Arr> obr;
Arr zlo(const Arr& a, const Arr& b, int n, int m)
{
    Arr c;
    rep(i, 0, n)rep(j, 0, m){
        if(~b[i][j].first)c[i][j] = a[b[i][j].first][b[i][j].second];
        else c[i][j] = zer<pair<int, int>>;
    }
    return c;
}
void calc(int c, int n, int m)
{
    if(c < 2)return;
    if(c % 2){
        calc(c-1, n, m);
        obr[c] = zlo(obr[c-1], obr[1], n, m);
        return;
    }
    calc(c/2, n, m);
    obr[c] = zlo(obr[c/2], obr[c/2], n, m);
}
std::int32_t main()
{
    std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios_base::sync_with_stdio(0);
    int n(in()), m(in());
    rep(i, 0, n)rep(j, 0, m)cin >> tab[i][j];
    int k(in());
    stack <int> x, y;
    x.push(0); y.push(0);
    vector <int> ruc;
    while(k--){
        int a(ruch(in<char>()));
        if(a == x.top() || a == y.top())continue;
        while(ruc.size() && abs(ruc.back()) == abs(a)){
            ruc.pop_back();
            if(a % 2)y.pop();
            else x.pop();
        }
        if(a == x.top() || a == y.top())continue;
        ruc.pb(a);
        if(a % 2)y.push(a);
        else x.push(a);
    }
    if(sz(ruc) <= 10){
        for(auto i: ruc)exec(i, n, m);
        rep(i, 0, n){
            rep(j, 0, m)cout << tab[i][j];
            cout << '\n';
        }
    }
    else{
        int s((sz(ruc)-2)%4+2);
        rep(i, 0, s)exec(ruc[i], n, m);
        s = (sz(ruc) - s)/4;
        obr[0] = {};
        rep(i, 0, n)rep(j, 0, m){
            if(tab[i][j] != '.')obr[0][i][j] = {i, j};
            else obr[0][i][j] = {-1, -1};
        }
        obr[1] = obr[0];
        rep(i, 0, 4)exec(ruc[sz(ruc)-4+i], n, m, obr[1]);
        calc(s, n, m);
        rep(i, 0, n){
            rep(j, 0, m){
                if(~obr[s][i][j].first)cout << tab[obr[s][i][j].first][obr[s][i][j].second];
                else cout << '.';
            }
            cout << '\n';
        }
    }
    return 0;
}