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

using namespace std;

int n,m;
string cycle = "";
pair<string,string> pmov;
vector<vector<char>> tab;
bool _log = 0;

template<typename T>
vector<vector<T>> move(vector<vector<T>> t, char d, T empty){
    vector<vector<T>> res;
    res.assign(n,vector<T>(m,empty));
    if(d == 'G'){
        for(int x = 0; x<m; x++){
            int ind = 0;
            for(int y = 0; y<n; y++)if(t[y][x] != empty){
                res[ind++][x] = t[y][x];
            }
        }
    }
    else if(d == 'D'){
        for(int x = 0; x<m; x++){
            int ind = n-1;
            for(int y = n-1; y>=0; y--)if(t[y][x] != empty){
                res[ind--][x] = t[y][x];
            }
        }
    }
    if(d == 'L'){
        for(int y = 0; y<n; y++){
            int ind = 0;
            for(int x = 0; x<m; x++)if(t[y][x] != empty){
                res[y][ind++] = t[y][x];
            }
        }
    }
    else if(d == 'P'){
        for(int y = 0; y<n; y++){
            int ind = m-1;
            for(int x = m-1; x>=0; x--)if(t[y][x] != empty){
                res[y][ind--] = t[y][x];
            }
        }
    }
    
    return res;
}
template vector<vector<char>> move<char>(vector<vector<char>> t, char d, char empty);
template vector<vector<int>> move<int>(vector<vector<int>> t, char d, int empty);

map<char,char> opp = {{'G','D'}, {'D','G'},{'L','P'},{'P','L'}};

template<typename T>
void print(vector<vector<T>> t){
    for(int y = 0; y<n; y++){
        for(int x = 0; x<m; x++){
            cout<<t[y][x];
            if(_log)cout<<" ";
        }
        cout<<"\n";
    }
}
template void print<char>(vector<vector<char>> t);
template void print<int>(vector<vector<int>> t);

void brutforce(vector<vector<char>> tab, string mov){
    for(char c:mov) tab = move(tab,c,'.');
    print(tab);
}

void add(string &str, char c, bool add_to_empty){
    if (str.size() != 0 && str.back() == opp[c])str.pop_back();
    else str += c;
    if (str.size() == 0 && add_to_empty)str += c;
}

pair<string,string> preprocess(string mov){
    string res = "";
    string must_be = "";
    int is_up = 0;
    int is_left = 0;
    for(int i = 0; i<mov.size(); i++){
        
        if(mov[i] == 'G' && is_up != 1){
            add(res,'G', must_be.size() == 0);
            is_up = 1;
        }
        if(mov[i] == 'D' && is_up != -1){
            add(res,'D',must_be.size() == 0);
            is_up = -1;
        }
        if(mov[i] == 'L' && is_left != 1){
            add(res,'L',must_be.size() == 0);
            is_left = 1;
        }
        if(mov[i] == 'P' && is_left != -1){
            add(res,'P',must_be.size() == 0);
            is_left = -1;
        }
        if(is_up != 0 && is_left != 0 && must_be.size() == 0){
            must_be = res;
            res = "";
        }
        //cout<<i<<" -> "<<mov[i]<<" | "<<is_up<<" "<<is_left<<" | "<<res<<" | "<<must_be<<"\n";
    }
    return {must_be , res};
}

map<int,pair<int,int>> poz;
vector<int> G;
vector<bool> was;
vector<vector<int>> cyc;
vector<vector<char>> res;
int cycle_count = 0;

void recycle_cycle(vector<int> c){
    auto cc = c;
    int shift = c.size() - cycle_count%c.size();
    rotate(c.begin(), c.begin()+shift, c.end());
    for(int i = 0; i<c.size(); i++){
        auto sp = poz[c[i]];  //start poz
        auto ep = poz[cc[i]]; //end poz
        res[ep.second][ep.first] = tab[sp.second][sp.first];
    }
    if(_log){
        cout<<"cyc - \n";
        for(int i = 0; i<c.size(); i++)cout<<c[i]<<" ";cout<<endl;
        for(int i = 0; i<c.size(); i++)cout<<cc[i]<<" ";cout<<endl;
    }
}

void make_graph(vector<vector<char>> t){
    vector<vector<int>> it;
    it.assign(n,vector<int>(m,0));
    int ind = 1;
    for(int y = 0; y<n; y++){
        for(int x = 0; x<m; x++)if(t[y][x] != '.'){
            it[y][x] = ind;
            poz[ind++] = {x,y};
        }
    }
    G.assign(ind,0);
    was.assign(ind,0); 
    auto it2 = it;
    for(char c : cycle)it2 = move(it2, c, 0);
    for(int y = 0; y<n; y++){
        for(int x = 0; x<m; x++)if(t[y][x] != '.'){
            G[it2[y][x]] = it[y][x];
        }
    }
    
    res.assign(n,vector<char>(m,'.'));
    if(_log)cout<<ind<<" - ind\n";
    for(int i = 1; i<ind; i++)if(was[i] == 0){
        if(_log)cout<<i<<" ";
        was[i] = 1;
        int on_way = G[i];
        cyc.push_back(vector<int>());
        cyc.back().push_back(i);
        while(on_way != i){
            was[on_way] = 1;
            cyc.back().push_back(on_way);
            on_way = G[on_way];
        }
        recycle_cycle(cyc.back());
    }
}



int main(){
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    tab.assign(n,vector<char>(m,'.'));
    for(int y = 0; y<n; y++){
        for(int x = 0; x<m; x++){
            cin>>tab[y][x];
        }
    }
    string mov;
    int po_co_to_to;
    cin>>po_co_to_to>>mov;
    
    //brutforce(tab,mov);

    pmov = preprocess(mov);
    if(_log)cout<<pmov.first<<" "<<pmov.second<<"\n";
    for(char c: pmov.first)tab = move(tab,c,'.');
    if(pmov.second.size() <= 8){
        for(char c: pmov.second)tab = move(tab,c,'.');
        print(tab);
        return 0;
    }
    
    for(int i = 0; i<4; i++)cycle += pmov.second[i];
    
    cycle_count = pmov.second.size()/4;
    if(_log)cout<<cycle<<" "<<cycle_count<<" - cyc\n";
    if(_log)print(tab);
    make_graph(tab);
    for(int i = int(pmov.second.size()/4)*4; i < pmov.second.size(); i++){
        res = move(res,pmov.second[i],'.');
    }
    print(res);
    return 0;
}