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
// Autor: Adam Szymaszkiewicz
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

enum {PION, POZIOM};

int type(char a)
{
	if (a == 'D' || a == 'G')
		return PION;
	else
		return POZIOM;
}

string zip_next(const string &s)
{
	string ns;
	ns += s[0];
	for (int i = 1; i < s.size(); i++)
	{
		ns += s[i];
		if (type(ns.back()) == type(ns[ns.size()-2]))
		{
			char a = ns.back();
			ns.pop_back();
			ns.pop_back();
			ns += a;
		}
	}
	// cout << "I will return " << ns << "\n";
	return ns;
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<char>> v(n, vector<char>(m));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> v[i][j];

	int k;
	string s;
	cin >> k >> s;
	// cin >> s;
	while (1)
	{
		// cout << "\nnext iteration:\n";

		string ns = zip_next(s);
		// cout << "\tafter 1: " << ns << "\n";

		string ns2;
		ns2 += ns[0];
		for (int i = 1; i < ns.size(); i++)
		{
			ns2 += ns[i];
			if (i%2 == 0)
			{
				if (ns2.back() == ns2[ns2.size()-3])
					ns2.pop_back();
			}
		}
		// cout << "\tafter 2: " << ns2 << "\n";

		string ns3 = zip_next(ns2);
		// cout << "\tafter 3: " << ns3 << "  ns3.size=" << ns3.size() << "\n";

		string ns4;
		ns4 += ns3[0];
		if (ns3.size() > 1)
		{
			ns4 += ns3[1];
			for (int i = 2; i < ns3.size(); i++)
			{
				ns4 += ns3[i];
				if (i%2 == 1)
				{
					if (ns4.back() == ns4[ns4.size()-3])
						ns4.pop_back();
				}
			}
		}
		// cout << "\tafter 4: " << ns4 << "  ns4.size=" << ns4.size() << "\n\n";
		// cout << "czy rowne " << (ns3==ns4) << "\n";
		if (ns4 == s)
			break;

		// cout << "before s = \"" << s << "\" s.size() = " << s.size() << "\n";

		s = ns4;	

		// cout << "after s = \"" << s << "\" s.size() = " << s.size() << "\n";
	}

	k = s.size();
	for (int i = 0; i < k; i++)
	{
		int pion = 0, poziom = 0;
		char a = s[i];
		if (a == 'G')
			pion = 1;
		if (a == 'D')
			pion = -1;
		if (a == 'L')
			poziom = -1;
		if (a == 'P')
			poziom = 1;
		for (int i = 0; i < m; i++)
		{
			string s;
			int kropki = 0;
			for (int j = 0; j < n; j++)
			{
				if (v[j][i] == '.')
					kropki++;
				else
					s += v[j][i];
			}
			if (pion == 1)
			{
				s += string(kropki, '.');
				for (int j = 0; j < n; j++)
					v[j][i] = s[j];
			}
			if (pion == -1)
			{
				s = string(kropki, '.') + s;
				for (int j = 0; j < n; j++)
					v[j][i] = s[j];
			}
		}
		for (int i = 0; i < n; i++)
		{
			string s;
			int kropki = 0;
			for (int j = 0; j < m; j++)
			{
				if (v[i][j] == '.')
					kropki++;
				else
					s += v[i][j];
			}
			if (poziom == 1)
			{
				s = string(kropki, '.') + s;
				for (int j = 0; j < m; j++)
					v[i][j] = s[j];
			}
			if (poziom == -1)
			{
				s += string(kropki, '.');
				for (int j = 0; j < m; j++)
					v[i][j] = s[j];
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << v[i][j];
		cout << "\n";
	}
}