#include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back #ifndef DEBUG constexpr int ALPH = 26; #else constexpr int ALPH = 2; auto&operator<<(auto &o, array<int, ALPH> p) {o << "("; for(int i=0; i<ALPH; ++i) o<<p[i]<<", "; o<<")"; return o;} #endif int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<array<int, ALPH>> col(m), row(n); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { char c; cin >> c; col[j][c-'A']++; row[i][c-'A']++; } } debug(col); debug(row); vector<array<int, 3>> op; vector<bool> used_col(m), used_row(n); vector<int> col_diff(m), row_diff(n); for(int i = 0; i < m; ++i) for(int j = 0; j < ALPH; ++j) if(col[i][j] > 0) col_diff[i]++; for(int i = 0; i < n; ++i) for(int j = 0; j < ALPH; ++j) if(row[i][j] > 0) row_diff[i]++; debug(col_diff); debug(row_diff); auto find_col = [&]() { int idx = -1; for(int i = 0; i < m; ++i) if(!used_col[i] && col_diff[i] == 1) {idx = i; break;} if(idx == -1) return mp(-1, -1); int c; for(c = 0; col[idx][c] == 0; ++c) ; return mp(idx, c); }; auto find_row = [&]() { int idx = -1; for(int i = 0; i < n; ++i) if(!used_row[i] && row_diff[i] == 1) {idx = i; break;} if(idx == -1) return mp(-1, -1); int c; for(c = 0; row[idx][c] == 0; ++c) ; return mp(idx, c); }; cerr << endl; for(int i = 0; i < n+m; ++i) { auto [a, col1] = find_col(); auto [b, col2] = find_row(); debug(col_diff); debug(row_diff); debug(used_col); debug(used_row) debug(a); debug(b); if(a == -1 && b == -1) break; if(a != -1) { op.eb((array<int, 3>){0, a, col1}); used_col[a] = true; for(int j = 0; j < n; ++j) { if(--row[j][col1] == 0) row_diff[j]--; } } else { op.eb((array<int, 3>){1, b, col2}); used_row[b] = true; for(int j = 0; j < m; ++j) { if(--col[j][col2] == 0) col_diff[j]--; } } } cout << ssize(op) << '\n'; reverse(all(op)); for(auto [t, i, c] : op) { if(t == 0) cout << "K "; else cout << "R "; cout << i+1 << ' ' << char('A'+c) << '\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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back #ifndef DEBUG constexpr int ALPH = 26; #else constexpr int ALPH = 2; auto&operator<<(auto &o, array<int, ALPH> p) {o << "("; for(int i=0; i<ALPH; ++i) o<<p[i]<<", "; o<<")"; return o;} #endif int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<array<int, ALPH>> col(m), row(n); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { char c; cin >> c; col[j][c-'A']++; row[i][c-'A']++; } } debug(col); debug(row); vector<array<int, 3>> op; vector<bool> used_col(m), used_row(n); vector<int> col_diff(m), row_diff(n); for(int i = 0; i < m; ++i) for(int j = 0; j < ALPH; ++j) if(col[i][j] > 0) col_diff[i]++; for(int i = 0; i < n; ++i) for(int j = 0; j < ALPH; ++j) if(row[i][j] > 0) row_diff[i]++; debug(col_diff); debug(row_diff); auto find_col = [&]() { int idx = -1; for(int i = 0; i < m; ++i) if(!used_col[i] && col_diff[i] == 1) {idx = i; break;} if(idx == -1) return mp(-1, -1); int c; for(c = 0; col[idx][c] == 0; ++c) ; return mp(idx, c); }; auto find_row = [&]() { int idx = -1; for(int i = 0; i < n; ++i) if(!used_row[i] && row_diff[i] == 1) {idx = i; break;} if(idx == -1) return mp(-1, -1); int c; for(c = 0; row[idx][c] == 0; ++c) ; return mp(idx, c); }; cerr << endl; for(int i = 0; i < n+m; ++i) { auto [a, col1] = find_col(); auto [b, col2] = find_row(); debug(col_diff); debug(row_diff); debug(used_col); debug(used_row) debug(a); debug(b); if(a == -1 && b == -1) break; if(a != -1) { op.eb((array<int, 3>){0, a, col1}); used_col[a] = true; for(int j = 0; j < n; ++j) { if(--row[j][col1] == 0) row_diff[j]--; } } else { op.eb((array<int, 3>){1, b, col2}); used_row[b] = true; for(int j = 0; j < m; ++j) { if(--col[j][col2] == 0) col_diff[j]--; } } } cout << ssize(op) << '\n'; reverse(all(op)); for(auto [t, i, c] : op) { if(t == 0) cout << "K "; else cout << "R "; cout << i+1 << ' ' << char('A'+c) << '\n'; } return 0; } |