#include <bits/stdc++.h>
using namespace std;
#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)
#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int INF = 1e9 + 9;
struct Result {
char dim;
int number;
char color;
};
void inline one() {
int rows, cols;
cin >> rows >> cols;
vector<unordered_map<char, int>> rows_cnt(rows), cols_cnt(cols);
vector<string> t;
REP(i, rows) {
string s;
cin >> s;
t.emplace_back(s);
REP(j, cols) {
int c = s[j];
++rows_cnt[i][c];
++cols_cnt[j][c];
}
}
// DEB("rows_cnt:\n");
// REP(i, rows) {
// DEB("i = " << i << ":\n");
// for (const auto &[k, v] : rows_cnt[i]) {
// DEB(" (" << k << "," << v << ")");
// }
// DEB("\n");
// }
// DEB("cols_cnt:\n");
// REP(i, cols) {
// DEB("i = " << i << ":\n");
// for (const auto &[k, v] : cols_cnt[i]) {
// DEB(" (" << k << "," << v << ")");
// }
// DEB("\n");
// }
vector<Result> results;
for (;;) {
DEB("ITERATION\n");
bool any = false;
REP(row, rows) {
// DEB("row = " << row << ": cnt=" << SZ(rows_cnt[row]) << "\n");
// for (const auto &[k, v] : rows_cnt[row]) {
// DEB(" (" << k << "," << v << ")");
// }
// DEB("\n");
if (SZ(rows_cnt[row]) == 1) {
any = true;
const auto &it = rows_cnt[row].begin();
char color = it->first;
results.emplace_back('R', row, color);
rows_cnt[row].erase(it);
REP(col, cols) {
const auto &cnt = cols_cnt[col].find(color);
if (cnt != cols_cnt[col].end()) {
if (--cols_cnt[col][color] == 0) {
cols_cnt[col].erase(color);
}
}
}
}
}
REP(col, cols) {
// DEB("col = " << col << ": cnt=" << SZ(cols_cnt[col]) << "\n");
// for (const auto &[k, v] : cols_cnt[col]) {
// DEB(" (" << k << "," << v << ")");
// }
// DEB("\n");
if (SZ(cols_cnt[col]) == 1) {
any = true;
const auto &it = cols_cnt[col].begin();
char color = it->first;
results.emplace_back('K', col, color);
cols_cnt[col].erase(it);
REP(row, rows) {
const auto &cnt = rows_cnt[row].find(color);
if (cnt != rows_cnt[row].end()) {
if (--rows_cnt[row][color] == 0) {
rows_cnt[row].erase(color);
}
}
}
}
}
if (not any) {
break;
}
}
cout << SZ(results) << "\n";
reverse(results.begin(), results.end());
for (const auto &result : results) {
cout << result.dim << " " << (result.number + 1) << " " << result.color << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// int z; cin >> z; while(z--)
one();
}
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 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; struct Result { char dim; int number; char color; }; void inline one() { int rows, cols; cin >> rows >> cols; vector<unordered_map<char, int>> rows_cnt(rows), cols_cnt(cols); vector<string> t; REP(i, rows) { string s; cin >> s; t.emplace_back(s); REP(j, cols) { int c = s[j]; ++rows_cnt[i][c]; ++cols_cnt[j][c]; } } // DEB("rows_cnt:\n"); // REP(i, rows) { // DEB("i = " << i << ":\n"); // for (const auto &[k, v] : rows_cnt[i]) { // DEB(" (" << k << "," << v << ")"); // } // DEB("\n"); // } // DEB("cols_cnt:\n"); // REP(i, cols) { // DEB("i = " << i << ":\n"); // for (const auto &[k, v] : cols_cnt[i]) { // DEB(" (" << k << "," << v << ")"); // } // DEB("\n"); // } vector<Result> results; for (;;) { DEB("ITERATION\n"); bool any = false; REP(row, rows) { // DEB("row = " << row << ": cnt=" << SZ(rows_cnt[row]) << "\n"); // for (const auto &[k, v] : rows_cnt[row]) { // DEB(" (" << k << "," << v << ")"); // } // DEB("\n"); if (SZ(rows_cnt[row]) == 1) { any = true; const auto &it = rows_cnt[row].begin(); char color = it->first; results.emplace_back('R', row, color); rows_cnt[row].erase(it); REP(col, cols) { const auto &cnt = cols_cnt[col].find(color); if (cnt != cols_cnt[col].end()) { if (--cols_cnt[col][color] == 0) { cols_cnt[col].erase(color); } } } } } REP(col, cols) { // DEB("col = " << col << ": cnt=" << SZ(cols_cnt[col]) << "\n"); // for (const auto &[k, v] : cols_cnt[col]) { // DEB(" (" << k << "," << v << ")"); // } // DEB("\n"); if (SZ(cols_cnt[col]) == 1) { any = true; const auto &it = cols_cnt[col].begin(); char color = it->first; results.emplace_back('K', col, color); cols_cnt[col].erase(it); REP(row, rows) { const auto &cnt = rows_cnt[row].find(color); if (cnt != rows_cnt[row].end()) { if (--rows_cnt[row][color] == 0) { rows_cnt[row].erase(color); } } } } } if (not any) { break; } } cout << SZ(results) << "\n"; reverse(results.begin(), results.end()); for (const auto &result : results) { cout << result.dim << " " << (result.number + 1) << " " << result.color << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |
English