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
from sys import stdin, stdout



n, m = [int(x) for x in stdin.readline().split()]

table = []
for _ in range(n):
    table.append(list(stdin.readline().strip()))

k = int(stdin.readline())

moves = stdin.readline().strip()



def reduce_moves(moves):
    def move_type(c):
        if c in ["L", "P"]:
            return 0
        else:
            return 1
    res = []
    i = 0
    while i < len(moves):
        c = moves[i]
        if len(res) == 0:
            res.append(c)
            i += 1
        elif len(res) == 1:
            if move_type(c) == move_type(res[-1]):
                res[-1] = c
            else:
                res.append(c)
            i += 1
        else:
            if move_type(c) == move_type(res[-1]):
                res.pop()
            else:
                if c != res[-2]:
                    res.append(c)
                i += 1
    return res

def apply_move(directon, table):
    n, m = len(table), len(table[0])
    if directon == "G":
        nonempty_columns = [[] for _ in range(m)]
        for row in table:
            for i, c in enumerate(row):
                if c != ".":
                    nonempty_columns[i].append(c)
        full_columns = [
            column + ['.'] * (n - len(column)) for column in nonempty_columns
        ]
        res = [[] for _ in range(n)]
        for col in full_columns:
            for i, c in enumerate(col):
                res[i].append(c)
    if directon == "D":
        nonempty_columns = [[] for _ in range(m)]
        for row in table:
            for i, c in enumerate(row):
                if c != ".":
                    nonempty_columns[i].append(c)
        full_columns = [
            ['.'] * (n - len(column)) + column for column in nonempty_columns
        ]
        res = [[] for _ in range(n)]
        for col in full_columns:
            for i, c in enumerate(col):
                res[i].append(c)
    if directon == "L":
        nonempty = [
            [c for c in row if c !="."] for row in table
        ]
        res = [
            row + ['.'] * (m - len(row)) for row in nonempty
        ]
    if directon == "P":
        nonempty = [
            [c for c in row if c !="."] for row in table
        ]
        res = [
            ['.'] * (m - len(row)) + row for row in nonempty
        ]
    return res

def enumerator():
    i = 0
    while True:
        yield i
        i += 1

def get_number_table(table):
    enum = enumerator()
    res = []
    for row in table:
        res.append([c if c == "." else next(enum) for c in row])
    return res

def read_permutation(start_table, end_table):
    perm_dict = {}
    for start_row, end_row in zip(start_table, end_table):
        for start_c, end_c in zip(start_row, end_row):
            if start_c != ".":
                perm_dict[start_c] = end_c
    perm_list = [-1] * len(perm_dict)
    for k, v in perm_dict.items():
        perm_list[k] = v
    return perm_list

def to_cycles(perm):
    perm = perm.copy()
    res = []
    for i in range(len(perm)):
        if perm[i] == 0:
            continue # already visited
        cycle = [i]
        j = i
        while perm[j] != i:
            cycle.append(perm[j])
            next_j = perm[j]
            perm[j] = 0
            j = next_j
        perm[j] = 0 
        res.append(cycle)
    return res 

def from_cycles(cycles):
    res = [0] * sum(len(c) for c in cycles)
    for cycle in cycles:
        for i in range(len(cycle)):
            res[cycle[i]] = cycle[(i+1)%len(cycle)]
    return res

def compose_permutation(k, perm):
    cycles = to_cycles(perm)
    composed_cycles = []
    for cycle in cycles:
        n = len(cycle)
        cycle_k = k % n
        cycle_start = seen = 0
        composed_cycle = []
        i = cycle_k
        while seen < n:
            if i != cycle_start:
                composed_cycle.append(cycle[i])
                i += cycle_k
                i %= n
                seen += 1
                print(composed_cycle)
            else:
                composed_cycle.append(cycle[i])
                seen += 1
                composed_cycles.append(composed_cycle)
                cycle_start += 1
                i = (cycle_start + cycle_k) % n
                composed_cycle = []
    return from_cycles(composed_cycles)

def apply_permutation(perm, numbered_table, table):
    color_dict = []
    for number_row, color_row in zip(numbered_table, table):
        for number, color in zip(number_row, color_row):
            if number != ".":
                color_dict[number] = color
    res = []
    for number_row in numbered_table:
        res.append([color_dict[perm[c]] if c !="." else "." for c in number_row])
    return res



moves = reduce_moves(moves)

manual_start = 2 + (len(moves)+2)%4

if len(moves) <= manual_start:
    for c in moves:
        table = apply_move(c, table)
    res = table
else:
    for c in moves[:manual_start]:
        table = apply_move(c, table)
    numbered_table = get_number_table(table)
    permutation_table = numbered_table.copy()
    for c in moves[manual_part:manual_part+4]:
        permutation_table = apply_move(c, permutation_table)
    one_lap = read_permutation(numbered_table, permutation_table)
    assert (len(moves) - manual_part) % 4 == 0
    final_permutation = compose_permutation((len(moves) - manual_part)//4, one_lap)
    res = apply_permutation(final_permutation, numbered_table, table)

for row in table:
    stdout.write(" ".join(row)+"\n")