#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; int singular(const array<int, 26>& m) { int ct = 0, last = 0; rep (i, 0, 26) { if (m[i]) ct++, last = i; } if (ct == 0) return -1; if (ct == 1) return last; assert(ct > 1); return -2; } void print(const array<int, 26>& m) { rep (i, 0, 26) { if (m[i]) { char c = (char)('A'+i); debug(c, m[i]); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vec<array<int, 26>> rows(n), columns(m); rep (i, 0, n) rep (j, 0, 26) rows[i][j] = 0; rep (i, 0, m) rep (j, 0, 26) columns[i][j] = 0; vec<bool> usedrows(n), usedcolumns(m); rep (i, 0, n) { string s; cin >> s; rep (j, 0, m) { int c = s[j]-'A'; rows[i][c]++; columns[j][c]++; } } debug(rows); debug(columns); vec<int> qr, qc; // cout << n+m << "\n"; rep (i, 0, n) { if (singular(rows[i]) >= 0) { usedrows[i] = true; qr.push_back(i); } } rep (i, 0, m) { if (singular(columns[i]) >= 0) { usedcolumns[i] = true; qc.push_back(i); } } vec<pair<bool, pair<int, int>>> ops; while (qr.size() || qc.size()) { debug(ops.size()); if (ops.size()) { debug(ops.back()); } rep(i, 0, n) { debug("row", i); print(rows[i]); } rep(i, 0, m) { debug("col", i); print(columns[i]); } // debug(rows); // debug(columns); if (qr.size()) { int i = qr.back(); qr.pop_back(); int sing = singular(rows[i]); if (sing == -1) continue; int val = sing; rows[i][val] = 0; ops.push_back({false, {i, val}}); rep (j, 0, m) { columns[j][val] = max(columns[j][val]-1, 0); if (!usedcolumns[j] && singular(columns[j]) >= 0) { usedcolumns[j] = true; qc.push_back(j); } } } else if (qc.size()) { int i = qc.back(); qc.pop_back(); int sing = singular(columns[i]); if (sing == -1) continue; int val = sing; columns[i][val] = 0; ops.push_back({true, {i, val}}); rep (j, 0, n) { rows[j][val] = max(rows[j][val]-1, 0); if (!usedrows[j] && singular(rows[j]) >= 0) { usedrows[j] = true; qr.push_back(j); } } } } cout << ops.size() << "\n"; reverse(all(ops)); for (auto [a, x] : ops) { auto [b, c] = x; cout << (a ? "K" : "R") << " " << b+1 << " " << (char)(c+'A') << "\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 | #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; int singular(const array<int, 26>& m) { int ct = 0, last = 0; rep (i, 0, 26) { if (m[i]) ct++, last = i; } if (ct == 0) return -1; if (ct == 1) return last; assert(ct > 1); return -2; } void print(const array<int, 26>& m) { rep (i, 0, 26) { if (m[i]) { char c = (char)('A'+i); debug(c, m[i]); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vec<array<int, 26>> rows(n), columns(m); rep (i, 0, n) rep (j, 0, 26) rows[i][j] = 0; rep (i, 0, m) rep (j, 0, 26) columns[i][j] = 0; vec<bool> usedrows(n), usedcolumns(m); rep (i, 0, n) { string s; cin >> s; rep (j, 0, m) { int c = s[j]-'A'; rows[i][c]++; columns[j][c]++; } } debug(rows); debug(columns); vec<int> qr, qc; // cout << n+m << "\n"; rep (i, 0, n) { if (singular(rows[i]) >= 0) { usedrows[i] = true; qr.push_back(i); } } rep (i, 0, m) { if (singular(columns[i]) >= 0) { usedcolumns[i] = true; qc.push_back(i); } } vec<pair<bool, pair<int, int>>> ops; while (qr.size() || qc.size()) { debug(ops.size()); if (ops.size()) { debug(ops.back()); } rep(i, 0, n) { debug("row", i); print(rows[i]); } rep(i, 0, m) { debug("col", i); print(columns[i]); } // debug(rows); // debug(columns); if (qr.size()) { int i = qr.back(); qr.pop_back(); int sing = singular(rows[i]); if (sing == -1) continue; int val = sing; rows[i][val] = 0; ops.push_back({false, {i, val}}); rep (j, 0, m) { columns[j][val] = max(columns[j][val]-1, 0); if (!usedcolumns[j] && singular(columns[j]) >= 0) { usedcolumns[j] = true; qc.push_back(j); } } } else if (qc.size()) { int i = qc.back(); qc.pop_back(); int sing = singular(columns[i]); if (sing == -1) continue; int val = sing; columns[i][val] = 0; ops.push_back({true, {i, val}}); rep (j, 0, n) { rows[j][val] = max(rows[j][val]-1, 0); if (!usedrows[j] && singular(rows[j]) >= 0) { usedrows[j] = true; qr.push_back(j); } } } } cout << ops.size() << "\n"; reverse(all(ops)); for (auto [a, x] : ops) { auto [b, c] = x; cout << (a ? "K" : "R") << " " << b+1 << " " << (char)(c+'A') << "\n"; } return 0; } |