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
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pb push_back
#define FOR(i,n) for(int i=0; i<n; i++)
#define watch(x) cout << (#x) << " == " << (x) << endl
typedef long long ll;
typedef long double ld;
using namespace std;
char tab[505][505];
bool dn[500005];
int h,w;
string s;

void move_puzzle(const char &c){
  if (c == 'D'){
    FOR (x,w){
      int lowest = h-1;
      for (int y=h-1; y>=0; y--){
        if (tab[y][x] != '.'){
          swap(tab[lowest][x], tab[y][x]);
          lowest--;
        }
      }
    }
  } else if (c == 'G'){
      FOR (x,w){
        int highest = 0;
        for (int y=0; y<h; y++){
          if (tab[y][x] != '.'){
            swap(tab[highest][x], tab[y][x]);
            highest++;
          }
        }
      }
  } else if (c == 'P'){
      FOR(y,h){
        int rightmost = w-1;
        for (int x=w-1; x>=0; x--){
          if (tab[y][x] != '.'){
            swap(tab[y][rightmost], tab[y][x]);
            rightmost--;
          }
        }
      }
  } else {
      FOR(y,h){
        int leftmost = 0;
        for (int x=0; x<w; x++){
          if (tab[y][x] != '.'){
            swap(tab[y][leftmost], tab[y][x]);
            leftmost++;
          }
        }
      }
  }
}

void chg(char &k1, char &k2){
  bool c=0;
  FOR(i, s.size()){
    if (dn[i]){
      if (s[i]==k1){
        if (c==1) {
          dn[i]=0;
        } else {
          c=1;
        }
      } else if (s[i]==k2) {c=0;}
    }
  }
}

void print_puzzle(){
  FOR(y,h){
    FOR(x,w){
      cout<<tab[y][x];
    }
    cout<<"\n";
  }
}


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

cin>>h>>w;

FOR(y,h){
  cin>>s;
  FOR(x,w){
    tab[y][x]=s[x];
  }
}

int k;
cin>>k>>s;

fill(dn,dn+s.size(),1);

FOR(i, s.size()){
  int j=i;
  if (s[i]=='D' || s[i]=='G'){
    while (j<s.size() && (s[j]=='D' || s[j]=='G')){
      j++;
    }
    for (int a=i+1; a<=j-2; a++){
      dn[a]=0;
    }
  }
  else if (s[i]=='L' || s[i]=='P'){
    while (j<s.size() && (s[j]=='L' || s[j]=='P')){
      j++;
    }
    for (int a=i+1; a<=j-2; a++){
      dn[a]=0;
    }
  }
  i=j-1;
}

char kier[4]={'G','D','L','P'};
chg(kier[0], kier[1]);
chg(kier[1], kier[0]);
chg(kier[2], kier[3]);
chg(kier[3], kier[2]);

FOR(i,k){
  if (dn[i])
  move_puzzle(s[i]);
}

print_puzzle();

return 0;
}