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
// Author: Bartek Knapik
#include <cstdio>

char board[501][501];
char all_moves[300009];
char moves_[300009];
char moves[300009];
int r, c, len_all, len_, len_moves;

void move(char mode)
{
    char letters[501];
    int n_letters;

    if (mode == 'G')
    {
        for (int nc = 0; nc < c; ++nc)
        {
            n_letters = 0;
            for (int i = 0; i < r; ++i) if (board[i][nc] != '.') letters[n_letters++] = board[i][nc];
            for (int i = 0; i < n_letters; ++i) board[i][nc] = letters[i];
            for (int i = n_letters; i < r; ++i) board[i][nc] = '.';
        }
    }
    else if (mode == 'D')
    {
        for (int nc = 0; nc < c; ++nc)
        {
            n_letters = 0;
            for (int i = 0; i < r; ++i) if (board[i][nc] != '.') letters[n_letters++] = board[i][nc];
            for (int i = 0; i < r - n_letters; ++i) board[i][nc] = '.';
            for (int i = r - n_letters; i < r; ++i) board[i][nc] = letters[i - r + n_letters];
        }
    }
    else if (mode == 'L')
    {
        for (int nr = 0; nr < r; ++nr)
        {
            n_letters = 0;
            for (int i = 0; i < c; ++i) if (board[nr][i] != '.') letters[n_letters++] = board[nr][i];
            for (int i = 0; i < n_letters; ++i) board[nr][i] = letters[i];
            for (int i = n_letters; i < c; ++i) board[nr][i] = '.';
        }
    }
    else 
    {
        for (int nr = 0; nr < r; ++nr)
        {
            n_letters = 0;
            for (int i = 0; i < c; ++i) if (board[nr][i] != '.') letters[n_letters++] = board[nr][i];
            for (int i = 0; i < c - n_letters; ++i) board[nr][i] = '.';
            for (int i = c - n_letters; i < c; ++i) board[nr][i] = letters[i - c + n_letters];
        }
    }
}

int main()
{
    scanf("%d%d", &r, &c);

    for (int i = 0; i < r; ++i)
        scanf("%s", &board[i]);

    scanf("%d", &len_all);
    scanf("%s", all_moves);

    moves_[0] = all_moves[0];
    len_ = 1;

    for (int i = 1; i < len_all; ++i)
    {
        if (moves_[len_ - 1] == all_moves[i])
            continue;
        moves_[len_++] = all_moves[i];
    }

    int ver = 0;
    int hor = 0;
    char ver_s = 0;
    char hor_s = 0;
    len_moves = 0;

    for (int i = 0; i < len_; ++i)
    {
        if (moves_[i] == 'D')
        {
            if (ver_s == moves_[i])
                continue;
            ver_s = moves_[i];
            if (ver > 1 && len_moves > 4 && moves[len_moves - 1] == 'G')
                len_moves--;
            else
            {
                moves[len_moves++] = moves_[i];
                ver++;
            }
        }
        else if (moves_[i] == 'G')
        {
            if (ver_s == moves_[i])
                continue;
            ver_s = moves_[i];
            if (ver > 1 && len_moves > 4 && moves[len_moves - 1] == 'D')
                len_moves--;
            else
            {
                moves[len_moves++] = moves_[i];
                ver++;
            }
        }
        else if (moves_[i] == 'L')
        {
            if (hor_s == moves_[i])
                continue;
            hor_s = moves_[i];
            if (hor > 1 && len_moves > 4 && moves[len_moves - 1] == 'P')
                len_moves--;
            else
            {
                moves[len_moves++] = moves_[i];
                hor++;
            }
        }
        else
        {
            if (hor_s == moves_[i])
                continue;
            hor_s = moves_[i];
            if (hor > 1 && len_moves > 4 && moves[len_moves - 1] == 'L')
                len_moves--;
            else
            {
                moves[len_moves++] = moves_[i];
                hor++;
            }
        }
    }

    for (int i = 0; i < len_moves; ++i)
        move(moves[i]);

    for (int i = 0; i < r; ++i)
        printf("%s\n", board[i]);

    return 0;

}