#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<array<int, 26>> rowds(n), colds(m); int rcnt = m, ccnt = n; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rowds[i][a[i][j] - 'A']++; colds[j][a[i][j] - 'A']++; } } vector<int> visR(n), visC(m); vector<array<int, 3>> ans; while (rcnt > 0 && ccnt > 0) { bool ok = 0; for (int j = 0; j < n; j++) { if (!visR[j] && *max_element(all(rowds[j])) == rcnt) { visR[j] = 1; ok = 1; for (int k = 0; k < 26; k++) { if (rowds[j][k] == rcnt) { ans.push_back({0, j, k}); break; } } for (int k = 0; k < m; k++) { rowds[j][a[j][k] - 'A']--; colds[k][a[j][k] - 'A']--; } ccnt--; break; } } if (ok) continue; for (int j = 0; j < m; j++) { if (!visC[j] && *max_element(all(colds[j])) == ccnt) { // ok, eliminate j visC[j] = 1; ok = 1; for (int k = 0; k < 26; k++) { if (colds[j][k] == ccnt) { ans.push_back({1, j, k}); break; } } for (int k = 0; k < n; k++) { rowds[k][a[k][j] - 'A']--; colds[j][a[k][j] - 'A']--; } rcnt--; break; } } if (ok) continue; assert(0); } reverse(all(ans)); cout << sz(ans) << "\n"; for (auto &[x, y, z] : ans) { cout << "RK"[x] << " " << y + 1 << " " << (char)('A' + z) << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<array<int, 26>> rowds(n), colds(m); int rcnt = m, ccnt = n; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rowds[i][a[i][j] - 'A']++; colds[j][a[i][j] - 'A']++; } } vector<int> visR(n), visC(m); vector<array<int, 3>> ans; while (rcnt > 0 && ccnt > 0) { bool ok = 0; for (int j = 0; j < n; j++) { if (!visR[j] && *max_element(all(rowds[j])) == rcnt) { visR[j] = 1; ok = 1; for (int k = 0; k < 26; k++) { if (rowds[j][k] == rcnt) { ans.push_back({0, j, k}); break; } } for (int k = 0; k < m; k++) { rowds[j][a[j][k] - 'A']--; colds[k][a[j][k] - 'A']--; } ccnt--; break; } } if (ok) continue; for (int j = 0; j < m; j++) { if (!visC[j] && *max_element(all(colds[j])) == ccnt) { // ok, eliminate j visC[j] = 1; ok = 1; for (int k = 0; k < 26; k++) { if (colds[j][k] == ccnt) { ans.push_back({1, j, k}); break; } } for (int k = 0; k < n; k++) { rowds[k][a[k][j] - 'A']--; colds[j][a[k][j] - 'A']--; } rcnt--; break; } } if (ok) continue; assert(0); } reverse(all(ans)); cout << sz(ans) << "\n"; for (auto &[x, y, z] : ans) { cout << "RK"[x] << " " << y + 1 << " " << (char)('A' + z) << "\n"; } } |