#include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 2007; int n, m, removed_rows, removed_cols; int in_row[26][NMAX], in_col[26][NMAX]; string t[NMAX]; string g[NMAX]; vector<string> res; void selfcheck() { for (int i = 0; i < n; ++i) { g[i].resize(m); } reverse(res.begin(), res.end()); for (auto &s : res) { // split s by space stringstream ss(s); char rk; int idx; char color; ss >> rk >> idx >> color; idx--; if (rk == 'K') { for (int i = 0; i < n; ++i) { g[i][idx] = color; } } else { for (int i = 0; i < m; ++i) { g[idx][i] = color; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { assert(t[i][j] == g[i][j]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> t[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { in_row[t[i][j] - 'A'][i]++; in_col[t[i][j] - 'A'][j]++; } } while (removed_cols < m && removed_rows < n) { int max_r = 0, max_c = 0, imr = 0, imc = 0, lc, lr; for (int i = 0; i < m; ++i) { for (int j = 0; j < 26; ++j) { if (max_c < in_col[j][i]) { max_c = in_col[j][i]; imc = i; lc = j; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < 26; ++j) { if (max_r < in_row[j][i]) { max_r = in_row[j][i]; imr = i; lr = j; } } } DEBUG( // cout << max_r << " " << max_c << " " << imr << " " << imc << "\n"; if (res.size() == 2) { for (int i = 0; i < n; ++i) { cout << in_row[0][i] << " "; } cout << "\n"; for (int i = 0; i < n; ++i) { cout << in_row[1][i] << " "; } cout << "\n"; for (int i = 0; i < n; ++i) { cout << in_row[2][i] << " "; } cout << "\n"; cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[0][i] << " "; } cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[1][i] << " "; } cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[2][i] << " "; } cout << "\n"; } ); if (max_c == n - removed_rows) { char c = lc + 'A'; res.push_back("K " + to_string(imc + 1) + ' ' + c); removed_cols++; for (int i = 0; i < n; ++i) { in_row[t[i][imc] - 'A'][i]--; } for (int i = 0; i < 26; ++i) { in_col[i][imc] = -1; } } else { char c = lr + 'A'; res.push_back("R " + to_string(imr + 1) + ' ' + c); removed_rows++; for (int i = 0; i < m; ++i) { in_col[t[imr][i] - 'A'][i]--; } for (int i = 0; i < 26; ++i) { in_row[i][imr] = -1; } } } cout << res.size() << "\n"; for (int i = res.size() - 1; i >= 0; --i) { cout << res[i] << "\n"; } // selfcheck(); DEBUG( cout.flush(); selfcheck(); ); 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 2007; int n, m, removed_rows, removed_cols; int in_row[26][NMAX], in_col[26][NMAX]; string t[NMAX]; string g[NMAX]; vector<string> res; void selfcheck() { for (int i = 0; i < n; ++i) { g[i].resize(m); } reverse(res.begin(), res.end()); for (auto &s : res) { // split s by space stringstream ss(s); char rk; int idx; char color; ss >> rk >> idx >> color; idx--; if (rk == 'K') { for (int i = 0; i < n; ++i) { g[i][idx] = color; } } else { for (int i = 0; i < m; ++i) { g[idx][i] = color; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { assert(t[i][j] == g[i][j]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> t[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { in_row[t[i][j] - 'A'][i]++; in_col[t[i][j] - 'A'][j]++; } } while (removed_cols < m && removed_rows < n) { int max_r = 0, max_c = 0, imr = 0, imc = 0, lc, lr; for (int i = 0; i < m; ++i) { for (int j = 0; j < 26; ++j) { if (max_c < in_col[j][i]) { max_c = in_col[j][i]; imc = i; lc = j; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < 26; ++j) { if (max_r < in_row[j][i]) { max_r = in_row[j][i]; imr = i; lr = j; } } } DEBUG( // cout << max_r << " " << max_c << " " << imr << " " << imc << "\n"; if (res.size() == 2) { for (int i = 0; i < n; ++i) { cout << in_row[0][i] << " "; } cout << "\n"; for (int i = 0; i < n; ++i) { cout << in_row[1][i] << " "; } cout << "\n"; for (int i = 0; i < n; ++i) { cout << in_row[2][i] << " "; } cout << "\n"; cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[0][i] << " "; } cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[1][i] << " "; } cout << "\n"; for (int i = 0; i < m; ++i) { cout << in_col[2][i] << " "; } cout << "\n"; } ); if (max_c == n - removed_rows) { char c = lc + 'A'; res.push_back("K " + to_string(imc + 1) + ' ' + c); removed_cols++; for (int i = 0; i < n; ++i) { in_row[t[i][imc] - 'A'][i]--; } for (int i = 0; i < 26; ++i) { in_col[i][imc] = -1; } } else { char c = lr + 'A'; res.push_back("R " + to_string(imr + 1) + ' ' + c); removed_rows++; for (int i = 0; i < m; ++i) { in_col[t[imr][i] - 'A'][i]--; } for (int i = 0; i < 26; ++i) { in_row[i][imr] = -1; } } } cout << res.size() << "\n"; for (int i = res.size() - 1; i >= 0; --i) { cout << res[i] << "\n"; } // selfcheck(); DEBUG( cout.flush(); selfcheck(); ); return 0; } |