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
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 500;
const int MAXK = 500000;

int n, m;
char tab[MAXN][MAXN];


void wypisz() {
    for(int in = 0; in<n; in++) {
      for(int im = 0; im<m; im++)
         cout << tab[in][im];
      cout << endl;
    }
}

void rusz_gora() {
    for(int im = 0; im < m; im++) {
        int cur_rd = 0;
        int cur_wr = 0;
        for( ; cur_rd < n; cur_rd++) {
            if(tab[cur_rd][im] != '.')
              tab[cur_wr++][im] = tab[cur_rd][im];
        }
        for( ; cur_wr < n; cur_wr++) {
              tab[cur_wr][im] = '.';
        }
    }
}

void rusz_dol() {
    for(int im = 0; im < m; im++) {
        int cur_rd = n-1;
        int cur_wr = n-1;
        for( ; cur_rd >= 0; cur_rd--) {
            if(tab[cur_rd][im] != '.')
              tab[cur_wr--][im] = tab[cur_rd][im];
        }
        for( ; cur_wr >= 0; cur_wr--) {
              tab[cur_wr][im] = '.';
        }
    }
}



void rusz_lewo() {
    for(int in = 0; in < n; in++) {
        int cur_rd = 0;
        int cur_wr = 0;
        for( ; cur_rd < m; cur_rd++) {
            if(tab[in][cur_rd] != '.')
              tab[in][cur_wr++] = tab[in][cur_rd];
        }
        for( ; cur_wr < m; cur_wr++) {
              tab[in][cur_wr] = '.';
        }
    }
}

void rusz_prawo() {
    for(int in = 0; in < n; in++) {
        int cur_rd = m-1;
        int cur_wr = m-1;
        for( ; cur_rd >= 0; cur_rd--) {
            if(tab[in][cur_rd] != '.')
              tab[in][cur_wr--] = tab[in][cur_rd];
        }
        for( ; cur_wr >= 0; cur_wr--) {
              tab[in][cur_wr] = '.';
        }
    }
}


void rusz(char kierunek) {
    switch(kierunek) {
        case 'G':
            rusz_gora();
            break;
        case 'D':
            rusz_dol();
            break;
        case 'P':
            rusz_prawo();
            break;
        case 'L':
            rusz_lewo();
            break;
    }
}


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

    for(int in = 0; in<n; in++)
      for(int im = 0; im<m; im++)
         cin >> tab[in][im];
    
    int k;
    cin >> k;
    
    vector<char> ruchy;
    char curr;
    cin >> curr;

    ruchy.push_back(curr);
    
    for(int ik = 1; ik < k; ik++) {
        cin >> curr;
        
        if( curr=='G' || curr=='D' ) {
            if (ruchy.back()=='G' || ruchy.back()=='D') {
                ruchy.back() = curr;
            }
            else {
                ruchy.push_back(curr);
            }
            if(ruchy.size() >= 5 && ruchy[ ruchy.size()-3 ]==ruchy.back())
                ruchy.pop_back();
        }
        
        
        if( curr=='P' || curr=='L' ) {
            if (ruchy.back()=='P' || ruchy.back()=='L') {
                ruchy.back() = curr;
            }
            else {
                ruchy.push_back(curr);
            }
            if(ruchy.size() >= 5 && ruchy[ ruchy.size()-3 ]==ruchy.back())
                ruchy.pop_back();
        }
    }

    int effective_size = ruchy.size();
    if(effective_size>10) 
        effective_size = 4 + (effective_size%4);
    
    for(int i = 0; i < effective_size; i++)
        rusz(ruchy[i]);
    
    wypisz();

    return 0;
}