#include <iostream> #include <vector> #include <iterator> #include <map> #include <algorithm> int W,H; template <typename T, T guard='.'> struct Map { Map(){ init(); } static bool cmp(T a, T b) { return a == guard && b != guard; } static bool cmp2(T a, T b) { return b == guard && a != guard; } struct HIter { using iterator_category = std::forward_iterator_tag; using difference_type = int; using value_type = T; using pointer = T*; // or also value_type* using reference = T&; // or also value_type& reference operator*() const { return ptr->at(x,y); } pointer operator->() { return &ptr->at(x,y); } // Prefix increment HIter& operator++() { ++y; return *this; } HIter& operator--() { --y; return *this; } // Postfix increment HIter operator++(int) { HIter tmp = *this; ++(*this); return tmp; } HIter operator--(int) { HIter tmp = *this; --(*this); return tmp; } HIter operator+(difference_type dy) const { return {ptr, x, y+dy}; } HIter operator+=(difference_type dy) { y+=dy; return *this; } difference_type operator-(const HIter& b) const { return y - b.y; } HIter operator-=(difference_type dy) { y-=dy; return *this; } friend bool operator== (const HIter& a, const HIter& b) { return a.ptr == b.ptr && a.x == b.x && a.y == b.y; }; friend bool operator!= (const HIter& a, const HIter& b) { return !(a == b); }; Map* ptr; int x, y; }; void V1() { // right for (int y=0;y<H;++y) std::stable_sort(m.data()+y*W, m.data()+y*W+W, cmp); } void V2() { // left for (int y=0;y<H;++y) std::stable_sort(m.data()+y*W, m.data()+y*W+W, cmp2); } void H1() { // down for (int x=0;x<W;++x) std::stable_sort(HIter{this,x,0}, HIter{this,x,H}, cmp); } void H2() { // up for (int x=0;x<W;++x) std::stable_sort(HIter{this,x,0}, HIter{this,x,H}, cmp2); } void Apply(char c) { switch (c) { case 'G': H2(); break; case 'D': H1(); break; case 'L': V2(); break; case 'P': V1(); break; } } bool operator<(const Map& M) const { return m < M.m; } bool operator==(const Map& M) const { return m == M.m; } T& operator()(int x, int y) { return m[y*W+x]; } T operator()(int x, int y) const { return m[y*W+x]; } T& at(int x, int y) { return m[y*W+x]; } void init() { m.resize(W*H); } std::vector<T> m; }; std::ostream& operator<<(std::ostream& out, const Map<char>& m) { for (int y=0;y<H;++y) { for (int x=0;x<W;++x) out << m(x,y); out << "\n"; } return out; } struct Evo; std::map<Map<char>, Evo> G; struct Evo { std::map<Map<char>, Evo>::iterator g,d,l,p; Evo():g(G.end()), d(G.end()), l(G.end()), p(G.end()) {} std::map<Map<char>, Evo>::iterator& get(char c) { switch (c) { case 'G': return g; case 'D': return d; case 'L': return l; case 'P': return p; } return g; } }; char inv(char c) { switch (c) { case 'G': return 'D'; case 'D': return 'G'; case 'L': return 'P'; case 'P': return 'L'; } return '?'; } std::string reduce(const std::string& Q, int skip = 1) { if (Q.empty() || skip < 1) return ""; std::string O; char prev = 0; for (int i=0;i<Q.length();++i) { if (i < skip) O.push_back(Q[i]); else if (Q[i] == inv(O.back()) && prev != 0) { O.push_back(prev); O.push_back(Q[i]); prev = 0; // std::clog << " :: xD " << O << std::endl; } else if (Q[i] == inv(O.back())) { O.back() = Q[i]; } else if (Q[i] != O.back() && Q[i] != inv(O.back())) { // std::clog << " :: ?? " << Q[i] << " vs " << O.back() << " or " << inv(O.back()) << std::endl; prev = Q[i]; } } if (prev != 0) O.push_back(prev); return O; } Map<uint32_t,0xffffffff> inv(Map<uint32_t,0xffffffff> a) { Map<uint32_t,0xffffffff> b; b.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (a(x,y) == 0xffffffff) b(x,y) = 0xffffffff; else { int sx = a(x,y)&0xffff; int sy = a(x,y)>>16; b(sx,sy) = x | (y<<16); } return b; } Map<uint32_t,0xffffffff> sum(Map<uint32_t,0xffffffff> a, Map<uint32_t,0xffffffff> b) { Map<uint32_t,0xffffffff> c; c.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (a(x,y) == 0xffffffff) c(x,y) = 0xffffffff; else { int tx = a(x,y)&0xffff; int ty = a(x,y)>>16; c(x,y) = b(tx, ty); } return c; } Map<char> Solve(Map<char> m, std::string Q) { if (Q.empty()) return m; m.Apply(Q[0]); if (Q.length() == 1) return m; m.Apply(Q[1]); int i=2; while (Q.length() - i > 0 && (Q.length()-i)%4 != 0) { m.Apply(Q[i]); ++i; } // std::clog << " :: " << Q.length()-i << std::endl; Q = std::string(Q.begin()+i, Q.end()); if (Q.empty()) return m; // std::clog << Q << std::endl; int K = Q.length()/4; Map<uint32_t,0xffffffff> m2; m2.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m(x,y) == '.') m2(x,y) = 0xffffffff; else m2(x,y) = x | (y<<16); m2.Apply(Q[0]); m2.Apply(Q[1]); m2.Apply(Q[2]); m2.Apply(Q[3]); Map<uint32_t,0xffffffff> mm = inv(m2); Map<uint32_t,0xffffffff> m0; for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m(x,y) == '.') m0(x,y) = 0xffffffff; else m0(x,y) = x | (y<<16); // for (int i=0;i<K;++i) // m0 = sum(m0, mm); while (K > 0) { if (K&1) m0 = sum(m0, mm); mm = sum(mm, mm); K/=2; } Map<char> m_final = m; for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m0(x,y) != 0xffffffff) { int tx = m0(x,y)&0xffff; int ty = m0(x,y)>>16; m_final(tx,ty) = m(x, y); } return m_final; // std::map<Map<char>, Evo>::iterator m_it; // bool success; // std::tie(m_it, success) = G.insert({std::move(m), Evo()}); // int ctr = 0; // int inserts = 0; // for (char c: Q) { // Evo& e = m_it->second; // if (e.get(c) != G.end()) { // m_it = e.get(c); // // std::clog << "OK" << std::endl; // } else { // m = m_it->first; // m.Apply(c); // std::tie(m_it, success) = G.insert({std::move(m), Evo()}); // e.get(c) = m_it; // // std::clog << "VV" << std::endl; // ++inserts; // } // // ++ctr; // // if (ctr % 100 == 0) // // std::clog << ctr << " :: " << c << " " << G.size() << " / " << inserts << std::endl; // // std::clog << m << std::endl; // } // return m_it->first; } int main () { std::ios_base::sync_with_stdio(0); std::cin >> H >> W; Map<char> m; m.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) std::cin >> m(x,y); int k; std::cin >> k; std::string Q; std::cin >> Q; // std::clog << Q << std::endl << std::endl; int len; do { len = Q.length(); Q = reduce(Q,1); Q = reduce(Q,2); } while (len > Q.length()); // std::clog << Q.length() << std::endl; // std::clog << Q << std::endl; // return 0; std::cout << Solve(m, Q) << std::endl; 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 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #include <iostream> #include <vector> #include <iterator> #include <map> #include <algorithm> int W,H; template <typename T, T guard='.'> struct Map { Map(){ init(); } static bool cmp(T a, T b) { return a == guard && b != guard; } static bool cmp2(T a, T b) { return b == guard && a != guard; } struct HIter { using iterator_category = std::forward_iterator_tag; using difference_type = int; using value_type = T; using pointer = T*; // or also value_type* using reference = T&; // or also value_type& reference operator*() const { return ptr->at(x,y); } pointer operator->() { return &ptr->at(x,y); } // Prefix increment HIter& operator++() { ++y; return *this; } HIter& operator--() { --y; return *this; } // Postfix increment HIter operator++(int) { HIter tmp = *this; ++(*this); return tmp; } HIter operator--(int) { HIter tmp = *this; --(*this); return tmp; } HIter operator+(difference_type dy) const { return {ptr, x, y+dy}; } HIter operator+=(difference_type dy) { y+=dy; return *this; } difference_type operator-(const HIter& b) const { return y - b.y; } HIter operator-=(difference_type dy) { y-=dy; return *this; } friend bool operator== (const HIter& a, const HIter& b) { return a.ptr == b.ptr && a.x == b.x && a.y == b.y; }; friend bool operator!= (const HIter& a, const HIter& b) { return !(a == b); }; Map* ptr; int x, y; }; void V1() { // right for (int y=0;y<H;++y) std::stable_sort(m.data()+y*W, m.data()+y*W+W, cmp); } void V2() { // left for (int y=0;y<H;++y) std::stable_sort(m.data()+y*W, m.data()+y*W+W, cmp2); } void H1() { // down for (int x=0;x<W;++x) std::stable_sort(HIter{this,x,0}, HIter{this,x,H}, cmp); } void H2() { // up for (int x=0;x<W;++x) std::stable_sort(HIter{this,x,0}, HIter{this,x,H}, cmp2); } void Apply(char c) { switch (c) { case 'G': H2(); break; case 'D': H1(); break; case 'L': V2(); break; case 'P': V1(); break; } } bool operator<(const Map& M) const { return m < M.m; } bool operator==(const Map& M) const { return m == M.m; } T& operator()(int x, int y) { return m[y*W+x]; } T operator()(int x, int y) const { return m[y*W+x]; } T& at(int x, int y) { return m[y*W+x]; } void init() { m.resize(W*H); } std::vector<T> m; }; std::ostream& operator<<(std::ostream& out, const Map<char>& m) { for (int y=0;y<H;++y) { for (int x=0;x<W;++x) out << m(x,y); out << "\n"; } return out; } struct Evo; std::map<Map<char>, Evo> G; struct Evo { std::map<Map<char>, Evo>::iterator g,d,l,p; Evo():g(G.end()), d(G.end()), l(G.end()), p(G.end()) {} std::map<Map<char>, Evo>::iterator& get(char c) { switch (c) { case 'G': return g; case 'D': return d; case 'L': return l; case 'P': return p; } return g; } }; char inv(char c) { switch (c) { case 'G': return 'D'; case 'D': return 'G'; case 'L': return 'P'; case 'P': return 'L'; } return '?'; } std::string reduce(const std::string& Q, int skip = 1) { if (Q.empty() || skip < 1) return ""; std::string O; char prev = 0; for (int i=0;i<Q.length();++i) { if (i < skip) O.push_back(Q[i]); else if (Q[i] == inv(O.back()) && prev != 0) { O.push_back(prev); O.push_back(Q[i]); prev = 0; // std::clog << " :: xD " << O << std::endl; } else if (Q[i] == inv(O.back())) { O.back() = Q[i]; } else if (Q[i] != O.back() && Q[i] != inv(O.back())) { // std::clog << " :: ?? " << Q[i] << " vs " << O.back() << " or " << inv(O.back()) << std::endl; prev = Q[i]; } } if (prev != 0) O.push_back(prev); return O; } Map<uint32_t,0xffffffff> inv(Map<uint32_t,0xffffffff> a) { Map<uint32_t,0xffffffff> b; b.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (a(x,y) == 0xffffffff) b(x,y) = 0xffffffff; else { int sx = a(x,y)&0xffff; int sy = a(x,y)>>16; b(sx,sy) = x | (y<<16); } return b; } Map<uint32_t,0xffffffff> sum(Map<uint32_t,0xffffffff> a, Map<uint32_t,0xffffffff> b) { Map<uint32_t,0xffffffff> c; c.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (a(x,y) == 0xffffffff) c(x,y) = 0xffffffff; else { int tx = a(x,y)&0xffff; int ty = a(x,y)>>16; c(x,y) = b(tx, ty); } return c; } Map<char> Solve(Map<char> m, std::string Q) { if (Q.empty()) return m; m.Apply(Q[0]); if (Q.length() == 1) return m; m.Apply(Q[1]); int i=2; while (Q.length() - i > 0 && (Q.length()-i)%4 != 0) { m.Apply(Q[i]); ++i; } // std::clog << " :: " << Q.length()-i << std::endl; Q = std::string(Q.begin()+i, Q.end()); if (Q.empty()) return m; // std::clog << Q << std::endl; int K = Q.length()/4; Map<uint32_t,0xffffffff> m2; m2.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m(x,y) == '.') m2(x,y) = 0xffffffff; else m2(x,y) = x | (y<<16); m2.Apply(Q[0]); m2.Apply(Q[1]); m2.Apply(Q[2]); m2.Apply(Q[3]); Map<uint32_t,0xffffffff> mm = inv(m2); Map<uint32_t,0xffffffff> m0; for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m(x,y) == '.') m0(x,y) = 0xffffffff; else m0(x,y) = x | (y<<16); // for (int i=0;i<K;++i) // m0 = sum(m0, mm); while (K > 0) { if (K&1) m0 = sum(m0, mm); mm = sum(mm, mm); K/=2; } Map<char> m_final = m; for (int y=0;y<H;++y) for (int x=0;x<W;++x) if (m0(x,y) != 0xffffffff) { int tx = m0(x,y)&0xffff; int ty = m0(x,y)>>16; m_final(tx,ty) = m(x, y); } return m_final; // std::map<Map<char>, Evo>::iterator m_it; // bool success; // std::tie(m_it, success) = G.insert({std::move(m), Evo()}); // int ctr = 0; // int inserts = 0; // for (char c: Q) { // Evo& e = m_it->second; // if (e.get(c) != G.end()) { // m_it = e.get(c); // // std::clog << "OK" << std::endl; // } else { // m = m_it->first; // m.Apply(c); // std::tie(m_it, success) = G.insert({std::move(m), Evo()}); // e.get(c) = m_it; // // std::clog << "VV" << std::endl; // ++inserts; // } // // ++ctr; // // if (ctr % 100 == 0) // // std::clog << ctr << " :: " << c << " " << G.size() << " / " << inserts << std::endl; // // std::clog << m << std::endl; // } // return m_it->first; } int main () { std::ios_base::sync_with_stdio(0); std::cin >> H >> W; Map<char> m; m.init(); for (int y=0;y<H;++y) for (int x=0;x<W;++x) std::cin >> m(x,y); int k; std::cin >> k; std::string Q; std::cin >> Q; // std::clog << Q << std::endl << std::endl; int len; do { len = Q.length(); Q = reduce(Q,1); Q = reduce(Q,2); } while (len > Q.length()); // std::clog << Q.length() << std::endl; // std::clog << Q << std::endl; // return 0; std::cout << Solve(m, Q) << std::endl; return 0; } |