#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif void solve() { int n, m; cin >> n >> m; vector<vi> rows_cnt(n, vi(26)), cols_cnt(m, vi(26)); vi dif_rows(n), dif_cols(m); vector<string> board(n); FOR(i, 0, n) { string s; cin >> s; for(auto &x: s) x -= 'A'; board[i] = s; FOR(j, 0, m) { if(rows_cnt[i][s[j]] == 0) dif_rows[i]++; rows_cnt[i][s[j]]++; if(cols_cnt[j][s[j]] == 0) dif_cols[j]++; cols_cnt[j][s[j]]++; } } vector<tuple<char, int, char>> ops; vector<pii> ready; FOR(i, 0, n) if(dif_rows[i] == 1) ready.pb({0, i}); FOR(i, 0, m) if(dif_cols[i] == 1) ready.pb({1, i}); set<pii> vis; FOR(it, 0, sz(ready)) { auto [is_col, ind] = ready[it]; if(vis.count({is_col, ind})) continue; vis.insert({is_col, ind}); int color = -1; if(is_col) { FOR(i, 0, n) if(board[i][ind] != 26) { color = board[i][ind]; rows_cnt[i][color]--; if(rows_cnt[i][color] == 0) dif_rows[i]--; if(dif_rows[i] == 1) ready.pb({0, i}); board[i][ind] = 26; } } else { FOR(i, 0, m) if(board[ind][i] != 26) { color = board[ind][i]; cols_cnt[i][color]--; if(cols_cnt[i][color] == 0) dif_cols[i]--; if(dif_cols[i] == 1) ready.pb({1, i}); board[ind][i] = 26; } } if(color != -1) ops.pb({is_col ? 'K' : 'R', ind + 1, color + 'A'}); } reverse(all(ops)); cout << sz(ops) << '\n'; for(auto &[x, y, z]: ops) cout << x << ' ' << y << ' ' << z << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif void solve() { int n, m; cin >> n >> m; vector<vi> rows_cnt(n, vi(26)), cols_cnt(m, vi(26)); vi dif_rows(n), dif_cols(m); vector<string> board(n); FOR(i, 0, n) { string s; cin >> s; for(auto &x: s) x -= 'A'; board[i] = s; FOR(j, 0, m) { if(rows_cnt[i][s[j]] == 0) dif_rows[i]++; rows_cnt[i][s[j]]++; if(cols_cnt[j][s[j]] == 0) dif_cols[j]++; cols_cnt[j][s[j]]++; } } vector<tuple<char, int, char>> ops; vector<pii> ready; FOR(i, 0, n) if(dif_rows[i] == 1) ready.pb({0, i}); FOR(i, 0, m) if(dif_cols[i] == 1) ready.pb({1, i}); set<pii> vis; FOR(it, 0, sz(ready)) { auto [is_col, ind] = ready[it]; if(vis.count({is_col, ind})) continue; vis.insert({is_col, ind}); int color = -1; if(is_col) { FOR(i, 0, n) if(board[i][ind] != 26) { color = board[i][ind]; rows_cnt[i][color]--; if(rows_cnt[i][color] == 0) dif_rows[i]--; if(dif_rows[i] == 1) ready.pb({0, i}); board[i][ind] = 26; } } else { FOR(i, 0, m) if(board[ind][i] != 26) { color = board[ind][i]; cols_cnt[i][color]--; if(cols_cnt[i][color] == 0) dif_cols[i]--; if(dif_cols[i] == 1) ready.pb({1, i}); board[ind][i] = 26; } } if(color != -1) ops.pb({is_col ? 'K' : 'R', ind + 1, color + 'A'}); } reverse(all(ops)); cout << sz(ops) << '\n'; for(auto &[x, y, z]: ops) cout << x << ' ' << y << ' ' << z << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; } |