#include <bits/stdc++.h> using namespace std; //{ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define all(v) (v).begin(),(v).end() #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PII pair<int,int> #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } }; //{ #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); //} int n, m, k; char s[505][505]; char w[5000005]; char w2[5000005]; char w3[5000005]; bool same_dir(char c1, char c2) { if (c1 == c2) return true; if (c1 == 'G') return (c2 == 'D'); if (c1 == 'D') return (c2 == 'G'); if (c1 == 'P') return (c2 == 'L'); if (c1 == 'L') return (c2 == 'P'); } void wykonajG() { FOR(j,0,m) { vector<char> v; FOR(i,0,n) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(i,0,n) s[i][j] = '.'; FOR(i,0,v.size()) s[i][j] = v[i]; } } void wykonajD() { FOR(j,0,m) { vector<char> v; FOR(i,0,n) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(i,0,n) s[i][j] = '.'; FOR(i,0,v.size()) s[i+n-v.size()][j] = v[i]; } } void wykonajL() { FOR(i,0,n) { vector<char> v; FOR(j,0,m) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(j,0,m) s[i][j] = '.'; FOR(j,0,v.size()) s[i][j] = v[j]; } } void wykonajP() { FOR(i,0,n) { vector<char> v; FOR(j,0,m) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(j,0,m) s[i][j] = '.'; FOR(j,0,v.size()) s[i][j+m-v.size()] = v[j]; } } void wykonaj(char C) { if (C == 'G') wykonajG(); if (C == 'D') wykonajD(); if (C == 'L') wykonajL(); if (C == 'P') wykonajP(); } int sl[505][505]; void wykonajIntG() { FOR(j,0,m) { vector<int> v; FOR(i,0,n) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(i,0,n) sl[i][j] = 0; FOR(i,0,v.size()) sl[i][j] = v[i]; } } void wykonajIntD() { FOR(j,0,m) { vector<int> v; FOR(i,0,n) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(i,0,n) sl[i][j] = 0; FOR(i,0,v.size()) sl[i+(n-v.size())][j] = v[i]; } } void wykonajIntL() { FOR(i,0,n) { vector<int> v; FOR(j,0,m) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(j,0,m) sl[i][j] = 0; FOR(j,0,v.size()) sl[i][j] = v[j]; } } void wykonajIntP() { FOR(i,0,n) { vector<int> v; FOR(j,0,m) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(j,0,m) sl[i][j] = 0; FOR(j,0,v.size()) sl[i][j+(m-v.size())] = v[j]; } } void wykonajInt(char C) { if (C == 'G') wykonajIntG(); if (C == 'D') wykonajIntD(); if (C == 'L') wykonajIntL(); if (C == 'P') wykonajIntP(); } vector<char> sklad; int perm[250005]; int pot[250005]; bool na_cyklu[250005]; int main() { read2(n,m); FOR(i,0,n) { scanf("%s", s[i]); } read(k); scanf("%s", w); int k = strlen(w); int pos = 0; FOR(i,0,k) { if (pos > 0 && same_dir(w2[pos-1], w[i])) { w2[pos-1] = w[i]; if (pos >= 3 && w2[pos-3] == w2[pos-1]) { pos--; } } else { w2[pos] = w[i]; if (!(pos >= 2 && w2[pos-2] == w2[pos])) { pos++; } } } /*w2[pos] = '\0'; cout << w2 << endl; */ if (pos <= 6) { FOR(i,0,pos) wykonaj(w2[i]); } else { wykonaj(w2[0]); wykonaj(w2[1]); sklad.pb('A'); int obrotow = (pos-2)/4; int per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (s[i][j] != '.') { per_n++; sl[i][j] = per_n; sklad.pb(s[i][j]); } else sl[i][j] = 0; } FOR(i,2,6) { wykonajInt(w2[i]); } per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (sl[i][j] != 0) { per_n++; perm[per_n] = sl[i][j]; } } vector<VI> cykle; FOR(i,1,per_n+1) na_cyklu[i] = false; FOR(i,1,per_n+1) { if (na_cyklu[i]) continue; VI cyc; cyc.pb(i); na_cyklu[i] = true; while (perm[cyc.back()] != cyc[0]) { int nowy = perm[cyc.back()]; na_cyklu[nowy] = true; cyc.pb(nowy); } cykle.pb(cyc); } FOR(i,0,cykle.size()) { FOR(j,0,cykle[i].size()) { int ja = cykle[i][j]; int nr = (j+obrotow)%cykle[i].size(); pot[ja] = cykle[i][nr]; } } per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (s[i][j] != '.') { per_n++; s[i][j] = sklad[pot[per_n]]; } } FOR(i,2+4*obrotow,pos) { wykonaj(w2[i]); } } FOR(i,0,n) { FOR(j,0,m) { putchar(s[i][j]); } putchar('\n'); } }
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #include <bits/stdc++.h> using namespace std; //{ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define all(v) (v).begin(),(v).end() #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PII pair<int,int> #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } }; //{ #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); //} int n, m, k; char s[505][505]; char w[5000005]; char w2[5000005]; char w3[5000005]; bool same_dir(char c1, char c2) { if (c1 == c2) return true; if (c1 == 'G') return (c2 == 'D'); if (c1 == 'D') return (c2 == 'G'); if (c1 == 'P') return (c2 == 'L'); if (c1 == 'L') return (c2 == 'P'); } void wykonajG() { FOR(j,0,m) { vector<char> v; FOR(i,0,n) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(i,0,n) s[i][j] = '.'; FOR(i,0,v.size()) s[i][j] = v[i]; } } void wykonajD() { FOR(j,0,m) { vector<char> v; FOR(i,0,n) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(i,0,n) s[i][j] = '.'; FOR(i,0,v.size()) s[i+n-v.size()][j] = v[i]; } } void wykonajL() { FOR(i,0,n) { vector<char> v; FOR(j,0,m) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(j,0,m) s[i][j] = '.'; FOR(j,0,v.size()) s[i][j] = v[j]; } } void wykonajP() { FOR(i,0,n) { vector<char> v; FOR(j,0,m) { if (s[i][j] != '.') v.pb(s[i][j]); } FOR(j,0,m) s[i][j] = '.'; FOR(j,0,v.size()) s[i][j+m-v.size()] = v[j]; } } void wykonaj(char C) { if (C == 'G') wykonajG(); if (C == 'D') wykonajD(); if (C == 'L') wykonajL(); if (C == 'P') wykonajP(); } int sl[505][505]; void wykonajIntG() { FOR(j,0,m) { vector<int> v; FOR(i,0,n) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(i,0,n) sl[i][j] = 0; FOR(i,0,v.size()) sl[i][j] = v[i]; } } void wykonajIntD() { FOR(j,0,m) { vector<int> v; FOR(i,0,n) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(i,0,n) sl[i][j] = 0; FOR(i,0,v.size()) sl[i+(n-v.size())][j] = v[i]; } } void wykonajIntL() { FOR(i,0,n) { vector<int> v; FOR(j,0,m) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(j,0,m) sl[i][j] = 0; FOR(j,0,v.size()) sl[i][j] = v[j]; } } void wykonajIntP() { FOR(i,0,n) { vector<int> v; FOR(j,0,m) { if (sl[i][j] != 0) v.pb(sl[i][j]); } FOR(j,0,m) sl[i][j] = 0; FOR(j,0,v.size()) sl[i][j+(m-v.size())] = v[j]; } } void wykonajInt(char C) { if (C == 'G') wykonajIntG(); if (C == 'D') wykonajIntD(); if (C == 'L') wykonajIntL(); if (C == 'P') wykonajIntP(); } vector<char> sklad; int perm[250005]; int pot[250005]; bool na_cyklu[250005]; int main() { read2(n,m); FOR(i,0,n) { scanf("%s", s[i]); } read(k); scanf("%s", w); int k = strlen(w); int pos = 0; FOR(i,0,k) { if (pos > 0 && same_dir(w2[pos-1], w[i])) { w2[pos-1] = w[i]; if (pos >= 3 && w2[pos-3] == w2[pos-1]) { pos--; } } else { w2[pos] = w[i]; if (!(pos >= 2 && w2[pos-2] == w2[pos])) { pos++; } } } /*w2[pos] = '\0'; cout << w2 << endl; */ if (pos <= 6) { FOR(i,0,pos) wykonaj(w2[i]); } else { wykonaj(w2[0]); wykonaj(w2[1]); sklad.pb('A'); int obrotow = (pos-2)/4; int per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (s[i][j] != '.') { per_n++; sl[i][j] = per_n; sklad.pb(s[i][j]); } else sl[i][j] = 0; } FOR(i,2,6) { wykonajInt(w2[i]); } per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (sl[i][j] != 0) { per_n++; perm[per_n] = sl[i][j]; } } vector<VI> cykle; FOR(i,1,per_n+1) na_cyklu[i] = false; FOR(i,1,per_n+1) { if (na_cyklu[i]) continue; VI cyc; cyc.pb(i); na_cyklu[i] = true; while (perm[cyc.back()] != cyc[0]) { int nowy = perm[cyc.back()]; na_cyklu[nowy] = true; cyc.pb(nowy); } cykle.pb(cyc); } FOR(i,0,cykle.size()) { FOR(j,0,cykle[i].size()) { int ja = cykle[i][j]; int nr = (j+obrotow)%cykle[i].size(); pot[ja] = cykle[i][nr]; } } per_n = 0; FOR(i,0,n) FOR(j,0,m) { if (s[i][j] != '.') { per_n++; s[i][j] = sklad[pot[per_n]]; } } FOR(i,2+4*obrotow,pos) { wykonaj(w2[i]); } } FOR(i,0,n) { FOR(j,0,m) { putchar(s[i][j]); } putchar('\n'); } } |