//#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof spk<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof spk<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) { return *this; } template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i + 1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define upgrade cin.tie(0)->sync_with_stdio(0); #define lowb(v,x) (lower_bound(all(v),(x))-v.begin()) #define uppb(v,x) (upper_bound(all(v),(x))-v.begin()) #define erase_duplicates(x) {sort(all(x));x.resize(unique(all(x))-x.begin());} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f - b.f, a.s - b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f + b.f, a.s + b.s); } template <class p, class q> void umin(p &a, const q &b) { if (a > b) a = b; } template <class p, class q> void umax(p &a, const q &b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int K = 20; const int L = 500005; const int N = 505; int my, mx, k; char t[N][N]; char str[L]; pair <int,int> from[N][N]; pair <int,int> init[N][N]; pair <int,int> jump[N][N][K + 1]; void append(vector <char>& vec, char c) { char a = ss(vec) > 0 ? vec[ss(vec) - 1] : 0; char b = ss(vec) > 1 ? vec[ss(vec) - 2] : 0; if (c == a) return; if (c == b) return; if (a != 0 && (c == 'L' || c == 'P') == (a == 'L' || a == 'P')){ vec.pop_back(); append(vec, c); return; } vec.pb(c); } void simulate(char dir) { if (dir == 'L' || dir == 'P'){ rep(y, 1, my){ vector < pair <int,int> > row; rep(x, 1, mx){ if (from[y][x] != mp(0, 0)){ row.pb(from[y][x]); from[y][x] = mp(0, 0); } } if (dir == 'L'){ reverse(all(row)); rep(x, 1, mx){ if (row.empty()) break; from[y][x] = row.back(); row.pop_back(); } } if (dir == 'P'){ per(x, 1, mx){ if (row.empty()) break; from[y][x] = row.back(); row.pop_back(); } } } } if (dir == 'G' || dir == 'D'){ rep(x, 1, mx){ vector < pair <int,int> > col; rep(y, 1, my){ if (from[y][x] != mp(0, 0)){ col.pb(from[y][x]); from[y][x] = mp(0, 0); } } if (dir == 'G'){ reverse(all(col)); rep(y, 1, my){ if (col.empty()) break; from[y][x] = col.back(); col.pop_back(); } } if (dir == 'D'){ per(y, 1, my){ if (col.empty()) break; from[y][x] = col.back(); col.pop_back(); } } } } } int main() { scanf("%d%d", &my, &mx); rep(y, 1, my) scanf("%s", t[y] + 1); scanf("%d", &k); scanf("%s", str + 1); vector <char> vec; rep(i, 1, k) append(vec, str[i]); int n = ss(vec); rep(i, 0, n - 1){ if (i + 4 < n){ assert(vec[i] == vec[i + 4]); } } reverse(all(vec)); rep(y, 1, my) rep(x, 1, mx){ from[y][x] = t[y][x] == '.' ? mp(0, 0) : mp(y, x); } int pw = 0; if (vec.size() <= 20) goto finish; rep(i, 1, 4){ simulate(vec.back()); vec.pop_back(); } rep(y, 1, my) rep(x, 1, mx){ if (from[y][x] != mp(0, 0)){ init[y][x] = from[y][x]; from[y][x] = {y, x}; } } rep(i, 1, 4) simulate(vec[ss(vec) - i]); rep(y, 1, my) rep(x, 1, mx) jump[y][x][0] = from[y][x]; rep(k, 0, K - 1) rep(y, 1, my) rep(x, 1, mx){ int jy = jump[y][x][k].f; int jx = jump[y][x][k].s; if (mp(jy, jx) == mp(0, 0)) continue; jump[y][x][k + 1] = jump[jy][jx][k]; assert(jump[y][x][k + 1] != mp(0, 0)); } while (vec.size() > 20){ vec.pop_back(); vec.pop_back(); vec.pop_back(); vec.pop_back(); pw += 1; } rep(y, 1, my) rep(x, 1, mx){ pair <int,int> pos = {y, x}; int p = pw; per(k, 0, K){ if (p >= (1 << k)){ pos = jump[pos.f][pos.s][k]; p -= (1 << k); } } from[y][x] = init[pos.f][pos.s]; } finish: assert(vec.size() <= 20); while (!vec.empty()){ simulate(vec.back()); vec.pop_back(); } rep(y, 1, my){ rep(x, 1, mx){ int fy = from[y][x].f; int fx = from[y][x].s; if (mp(fy, fx) == mp(0, 0)) printf("."); else printf("%c", t[fy][fx]); } printf("\n"); } return 0; }
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 | //#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof spk<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof spk<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) { return *this; } template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i + 1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define upgrade cin.tie(0)->sync_with_stdio(0); #define lowb(v,x) (lower_bound(all(v),(x))-v.begin()) #define uppb(v,x) (upper_bound(all(v),(x))-v.begin()) #define erase_duplicates(x) {sort(all(x));x.resize(unique(all(x))-x.begin());} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f - b.f, a.s - b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f + b.f, a.s + b.s); } template <class p, class q> void umin(p &a, const q &b) { if (a > b) a = b; } template <class p, class q> void umax(p &a, const q &b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int K = 20; const int L = 500005; const int N = 505; int my, mx, k; char t[N][N]; char str[L]; pair <int,int> from[N][N]; pair <int,int> init[N][N]; pair <int,int> jump[N][N][K + 1]; void append(vector <char>& vec, char c) { char a = ss(vec) > 0 ? vec[ss(vec) - 1] : 0; char b = ss(vec) > 1 ? vec[ss(vec) - 2] : 0; if (c == a) return; if (c == b) return; if (a != 0 && (c == 'L' || c == 'P') == (a == 'L' || a == 'P')){ vec.pop_back(); append(vec, c); return; } vec.pb(c); } void simulate(char dir) { if (dir == 'L' || dir == 'P'){ rep(y, 1, my){ vector < pair <int,int> > row; rep(x, 1, mx){ if (from[y][x] != mp(0, 0)){ row.pb(from[y][x]); from[y][x] = mp(0, 0); } } if (dir == 'L'){ reverse(all(row)); rep(x, 1, mx){ if (row.empty()) break; from[y][x] = row.back(); row.pop_back(); } } if (dir == 'P'){ per(x, 1, mx){ if (row.empty()) break; from[y][x] = row.back(); row.pop_back(); } } } } if (dir == 'G' || dir == 'D'){ rep(x, 1, mx){ vector < pair <int,int> > col; rep(y, 1, my){ if (from[y][x] != mp(0, 0)){ col.pb(from[y][x]); from[y][x] = mp(0, 0); } } if (dir == 'G'){ reverse(all(col)); rep(y, 1, my){ if (col.empty()) break; from[y][x] = col.back(); col.pop_back(); } } if (dir == 'D'){ per(y, 1, my){ if (col.empty()) break; from[y][x] = col.back(); col.pop_back(); } } } } } int main() { scanf("%d%d", &my, &mx); rep(y, 1, my) scanf("%s", t[y] + 1); scanf("%d", &k); scanf("%s", str + 1); vector <char> vec; rep(i, 1, k) append(vec, str[i]); int n = ss(vec); rep(i, 0, n - 1){ if (i + 4 < n){ assert(vec[i] == vec[i + 4]); } } reverse(all(vec)); rep(y, 1, my) rep(x, 1, mx){ from[y][x] = t[y][x] == '.' ? mp(0, 0) : mp(y, x); } int pw = 0; if (vec.size() <= 20) goto finish; rep(i, 1, 4){ simulate(vec.back()); vec.pop_back(); } rep(y, 1, my) rep(x, 1, mx){ if (from[y][x] != mp(0, 0)){ init[y][x] = from[y][x]; from[y][x] = {y, x}; } } rep(i, 1, 4) simulate(vec[ss(vec) - i]); rep(y, 1, my) rep(x, 1, mx) jump[y][x][0] = from[y][x]; rep(k, 0, K - 1) rep(y, 1, my) rep(x, 1, mx){ int jy = jump[y][x][k].f; int jx = jump[y][x][k].s; if (mp(jy, jx) == mp(0, 0)) continue; jump[y][x][k + 1] = jump[jy][jx][k]; assert(jump[y][x][k + 1] != mp(0, 0)); } while (vec.size() > 20){ vec.pop_back(); vec.pop_back(); vec.pop_back(); vec.pop_back(); pw += 1; } rep(y, 1, my) rep(x, 1, mx){ pair <int,int> pos = {y, x}; int p = pw; per(k, 0, K){ if (p >= (1 << k)){ pos = jump[pos.f][pos.s][k]; p -= (1 << k); } } from[y][x] = init[pos.f][pos.s]; } finish: assert(vec.size() <= 20); while (!vec.empty()){ simulate(vec.back()); vec.pop_back(); } rep(y, 1, my){ rep(x, 1, mx){ int fy = from[y][x].f; int fx = from[y][x].s; if (mp(fy, fx) == mp(0, 0)) printf("."); else printf("%c", t[fy][fx]); } printf("\n"); } return 0; } |