#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } char other(char ch) { if (ch == 'L') return 'P'; if (ch == 'P') return 'L'; if (ch == 'G') return 'D'; if (ch == 'D') return 'G'; assert(false); } string reduce(const string &seq) { string ans; for (char ch : seq) { debug(ch, ans, seq); if (ans.empty()) { ans.push_back(ch); continue; } if (ans.back() == ch || ans.back() == other(ch)) ans.back() = ch; else ans.push_back(ch); if (ans.size() >= 3 && ans[ans.size()-3] == ans.back()) ans.pop_back(); debug(ans); } return ans; } void rotate(vvi &b, char how) { int n = (int)b.size(), m = (int)b[0].size(); if (how == 'L') { rep(i, n) { int wh = 0; rep(j, m) { if (b[i][j] == -1) continue; swap(b[i][j], b[i][wh++]); } } } else if (how == 'P') { rep(i, n) { int wh = m-1; repr(j, m) { if (b[i][j] == -1) continue; swap(b[i][j], b[i][wh--]); } } } else if (how == 'G') { rep(j, m) { int wh = 0; rep(i, n) { if (b[i][j] == -1) continue; swap(b[i][j], b[wh++][j]); } } } else if (how == 'D') { rep(j, m) { int wh = n-1; repr(i, n) { if (b[i][j] == -1) continue; swap(b[i][j], b[wh--][j]); } } } else assert(false); } map<int, char> create_mapping(vvi &b) { map<int, char> mp; for (auto &a : b) for (int &i : a) { if (i == -1) continue; int id = (int)mp.size(); mp[id] = (char)i; i = id; } return mp; } void apply_mapping(vvi &b, map<int, char> &mp) { for (auto &a : b) for (int &i : a) { if (i == -1) continue; i = mp[i]; } } vi compose(const vi& a, const vi&b) { int n = (int)a.size(); vi p(n); rep(i, n) p[i] = a[b[i]]; // (it commutes in this case) return p; } vi permpow(const vi& a, int pp) { assert(pp > 0); if (pp == 1) return a; auto aa = compose(a, a); auto ax = permpow(aa, pp / 2); if (pp & 1) return compose(ax, a); else return ax; } void solve() { int n, m; cin >> n >> m; vvi b(n, vi(m)); for (auto& a : b) for (int &i : a) { char ch; cin >> ch; if (ch == '.') ch = -1; i = ch; } debug(b); int _x_; string seq; cin >> _x_ >> seq; seq = reduce(seq); if (seq.size() >= 1) rotate(b, seq[0]); if (seq.size() >= 2) rotate(b, seq[1]); const int to_ff = max((int(seq.size())-2) / 4, 0); if (to_ff) { auto mp = create_mapping(b); auto bcpy = b; debug(b); rotate(bcpy, seq[2]); rotate(bcpy, seq[3]); rotate(bcpy, seq[4]); rotate(bcpy, seq[5]); debug(bcpy); vi perm(mp.size()); rep(i, n) rep(j, m) { assert((b[i][j] == -1) == (bcpy[i][j] == -1)); if (b[i][j] == -1) continue; perm[ b[i][j] ] = bcpy[i][j]; } debug(perm, to_ff); perm = permpow(perm, to_ff); debug(b, perm, mp); rep(i, n) rep(j, m) { if (b[i][j] == -1) continue; b[i][j] = perm[ b[i][j] ]; } apply_mapping(b, mp); } for (int i = 2 + to_ff * 4; i < (int)seq.size(); ++i) rotate(b, seq[i]); rep(i, n) { rep(j, m) { char ch = b[i][j]; if (ch == -1) ch = '.'; cout << ch; } cout << '\n'; } debug(seq); } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } char other(char ch) { if (ch == 'L') return 'P'; if (ch == 'P') return 'L'; if (ch == 'G') return 'D'; if (ch == 'D') return 'G'; assert(false); } string reduce(const string &seq) { string ans; for (char ch : seq) { debug(ch, ans, seq); if (ans.empty()) { ans.push_back(ch); continue; } if (ans.back() == ch || ans.back() == other(ch)) ans.back() = ch; else ans.push_back(ch); if (ans.size() >= 3 && ans[ans.size()-3] == ans.back()) ans.pop_back(); debug(ans); } return ans; } void rotate(vvi &b, char how) { int n = (int)b.size(), m = (int)b[0].size(); if (how == 'L') { rep(i, n) { int wh = 0; rep(j, m) { if (b[i][j] == -1) continue; swap(b[i][j], b[i][wh++]); } } } else if (how == 'P') { rep(i, n) { int wh = m-1; repr(j, m) { if (b[i][j] == -1) continue; swap(b[i][j], b[i][wh--]); } } } else if (how == 'G') { rep(j, m) { int wh = 0; rep(i, n) { if (b[i][j] == -1) continue; swap(b[i][j], b[wh++][j]); } } } else if (how == 'D') { rep(j, m) { int wh = n-1; repr(i, n) { if (b[i][j] == -1) continue; swap(b[i][j], b[wh--][j]); } } } else assert(false); } map<int, char> create_mapping(vvi &b) { map<int, char> mp; for (auto &a : b) for (int &i : a) { if (i == -1) continue; int id = (int)mp.size(); mp[id] = (char)i; i = id; } return mp; } void apply_mapping(vvi &b, map<int, char> &mp) { for (auto &a : b) for (int &i : a) { if (i == -1) continue; i = mp[i]; } } vi compose(const vi& a, const vi&b) { int n = (int)a.size(); vi p(n); rep(i, n) p[i] = a[b[i]]; // (it commutes in this case) return p; } vi permpow(const vi& a, int pp) { assert(pp > 0); if (pp == 1) return a; auto aa = compose(a, a); auto ax = permpow(aa, pp / 2); if (pp & 1) return compose(ax, a); else return ax; } void solve() { int n, m; cin >> n >> m; vvi b(n, vi(m)); for (auto& a : b) for (int &i : a) { char ch; cin >> ch; if (ch == '.') ch = -1; i = ch; } debug(b); int _x_; string seq; cin >> _x_ >> seq; seq = reduce(seq); if (seq.size() >= 1) rotate(b, seq[0]); if (seq.size() >= 2) rotate(b, seq[1]); const int to_ff = max((int(seq.size())-2) / 4, 0); if (to_ff) { auto mp = create_mapping(b); auto bcpy = b; debug(b); rotate(bcpy, seq[2]); rotate(bcpy, seq[3]); rotate(bcpy, seq[4]); rotate(bcpy, seq[5]); debug(bcpy); vi perm(mp.size()); rep(i, n) rep(j, m) { assert((b[i][j] == -1) == (bcpy[i][j] == -1)); if (b[i][j] == -1) continue; perm[ b[i][j] ] = bcpy[i][j]; } debug(perm, to_ff); perm = permpow(perm, to_ff); debug(b, perm, mp); rep(i, n) rep(j, m) { if (b[i][j] == -1) continue; b[i][j] = perm[ b[i][j] ]; } apply_mapping(b, mp); } for (int i = 2 + to_ff * 4; i < (int)seq.size(); ++i) rotate(b, seq[i]); rep(i, n) { rep(j, m) { char ch = b[i][j]; if (ch == -1) ch = '.'; cout << ch; } cout << '\n'; } debug(seq); } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |