#include <bits/stdc++.h> using namespace std; const int N = 2'000 + 9; const int ALPH = 'Z' - 'A' + 10; int n,m; vector<vector<int>> rows; vector<vector<int>> cols; char find_first(vector<int> &t) { for (int i = 0; i < ALPH; i++) if (t[i] != 0) return 'A' + i; return 0; } int main() { // std::ios::sync_with_stdio(0); // std::cin.tie(0); cin >> n >> m; rows = vector<vector<int>> (n, vector<int> (ALPH, 0)); cols = vector<vector<int>> (m, vector<int> (ALPH, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; rows[i][c - 'A']++; cols[j][c - 'A']++; } } vector<tuple<char, int, char>> logs; vector<int> diff_row(n, 0); vector<int> diff_col(m, 0); vector<int> q_r; vector<int> q_c; vector<bool> v_r(n, false); vector<bool> v_c(m, false); for (int i = 0; i < n; i++) { for (int j = 0; j < ALPH; j++) diff_row[i] += (rows[i][j] != 0); if (diff_row[i] == 1) { q_r.push_back(i); v_r[i] = true; } } for (int i = 0; i < m; i++) { for (int j = 0; j < ALPH; j++) diff_col[i] += (cols[i][j] != 0); if (diff_col[i] == 1) { q_c.push_back(i); v_c[i] = true; } } while (!q_c.empty() || !q_r.empty()) { while (!q_r.empty()) { int x = q_r.back(); q_r.pop_back(); char ans = find_first(rows[x]); if (ans == 0) continue; logs.push_back({'R', x + 1, ans}); for (int j = 0; j < m; j++) { cols[j][ans - 'A']--; if (cols[j][ans - 'A'] == 0) diff_col[j]--; } } for (int j = 0; j < m; j++) { if (diff_col[j] == 1 && !v_c[j]) { q_c.push_back(j); v_c[j] = true; } } while (!q_c.empty()) { int x = q_c.back(); q_c.pop_back(); char ans = find_first(cols[x]); if (ans == 0) continue; logs.push_back({'K', x + 1, ans}); for (int j = 0; j < n; j++) { rows[j][ans - 'A']--; if (rows[j][ans - 'A'] == 0) diff_row[j]--; } } for (int j = 0; j < n; j++) { if (diff_row[j] == 1 && !v_r[j]) { q_r.push_back(j); v_r[j] = true; } } } cout << logs.size() << "\n"; for (auto it = logs.rbegin(); it != logs.rend(); it++) cout << get<0>(*it) << " " << get<1>(*it) << " " << get<2>(*it) << "\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 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 | #include <bits/stdc++.h> using namespace std; const int N = 2'000 + 9; const int ALPH = 'Z' - 'A' + 10; int n,m; vector<vector<int>> rows; vector<vector<int>> cols; char find_first(vector<int> &t) { for (int i = 0; i < ALPH; i++) if (t[i] != 0) return 'A' + i; return 0; } int main() { // std::ios::sync_with_stdio(0); // std::cin.tie(0); cin >> n >> m; rows = vector<vector<int>> (n, vector<int> (ALPH, 0)); cols = vector<vector<int>> (m, vector<int> (ALPH, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; rows[i][c - 'A']++; cols[j][c - 'A']++; } } vector<tuple<char, int, char>> logs; vector<int> diff_row(n, 0); vector<int> diff_col(m, 0); vector<int> q_r; vector<int> q_c; vector<bool> v_r(n, false); vector<bool> v_c(m, false); for (int i = 0; i < n; i++) { for (int j = 0; j < ALPH; j++) diff_row[i] += (rows[i][j] != 0); if (diff_row[i] == 1) { q_r.push_back(i); v_r[i] = true; } } for (int i = 0; i < m; i++) { for (int j = 0; j < ALPH; j++) diff_col[i] += (cols[i][j] != 0); if (diff_col[i] == 1) { q_c.push_back(i); v_c[i] = true; } } while (!q_c.empty() || !q_r.empty()) { while (!q_r.empty()) { int x = q_r.back(); q_r.pop_back(); char ans = find_first(rows[x]); if (ans == 0) continue; logs.push_back({'R', x + 1, ans}); for (int j = 0; j < m; j++) { cols[j][ans - 'A']--; if (cols[j][ans - 'A'] == 0) diff_col[j]--; } } for (int j = 0; j < m; j++) { if (diff_col[j] == 1 && !v_c[j]) { q_c.push_back(j); v_c[j] = true; } } while (!q_c.empty()) { int x = q_c.back(); q_c.pop_back(); char ans = find_first(cols[x]); if (ans == 0) continue; logs.push_back({'K', x + 1, ans}); for (int j = 0; j < n; j++) { rows[j][ans - 'A']--; if (rows[j][ans - 'A'] == 0) diff_row[j]--; } } for (int j = 0; j < n; j++) { if (diff_row[j] == 1 && !v_r[j]) { q_r.push_back(j); v_r[j] = true; } } } cout << logs.size() << "\n"; for (auto it = logs.rbegin(); it != logs.rend(); it++) cout << get<0>(*it) << " " << get<1>(*it) << " " << get<2>(*it) << "\n"; } |