#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pii>;
const int N = 2004;
char a[N][N];
int row_ile[26][N];
int row_ile_plus[N];
int col_ile[26][N];
int col_ile_plus[N];
vector<pair<pair<int, bool>, char>> ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
int idx = a[i][j] - 'A';
if (row_ile[idx][i]++ == 0) {
row_ile_plus[i]++;
}
if (col_ile[idx][j]++ == 0) {
col_ile_plus[j]++;
}
}
}
queue<pair<int, bool>> q;
const bool column = true;
const bool row = false;
for (int i = 0; i < n; i++) {
if (row_ile_plus[i] == 1) {
q.push({i, row});
}
}
for (int i = 0; i < m; i++) {
if (col_ile_plus[i] == 1) {
q.push({i, column});
}
}
while (!q.empty()) {
auto [i, type] = q.front();
q.pop();
char c;
if (type == column) {
for (int j = 0; j < 26; j++) {
if (col_ile[j][i] > 0) {
c = j + 'A';
break;
}
}
for (int j = 0; j < n; j++) {
if (--row_ile[a[j][i] - 'A'][j] == 0) {
if (--row_ile_plus[j] == 1) {
q.push({j, row});
}
}
}
} else {
for (int j = 0; j < 26; j++) {
if (row_ile[j][i] > 0) {
c = j + 'A';
break;
}
}
for (int j = 0; j < m; j++) {
if (--col_ile[a[i][j] - 'A'][j] == 0) {
if (--col_ile_plus[j] == 1) {
q.push({j, column});
}
}
}
}
ans.push_back({{i, type}, c});
}
reverse(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (auto p : ans) {
cout << (p.first.second == row ? "R" : "K") << " " << p.first.first + 1
<< " " << p.second << "\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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 2004; char a[N][N]; int row_ile[26][N]; int row_ile_plus[N]; int col_ile[26][N]; int col_ile_plus[N]; vector<pair<pair<int, bool>, char>> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; int idx = a[i][j] - 'A'; if (row_ile[idx][i]++ == 0) { row_ile_plus[i]++; } if (col_ile[idx][j]++ == 0) { col_ile_plus[j]++; } } } queue<pair<int, bool>> q; const bool column = true; const bool row = false; for (int i = 0; i < n; i++) { if (row_ile_plus[i] == 1) { q.push({i, row}); } } for (int i = 0; i < m; i++) { if (col_ile_plus[i] == 1) { q.push({i, column}); } } while (!q.empty()) { auto [i, type] = q.front(); q.pop(); char c; if (type == column) { for (int j = 0; j < 26; j++) { if (col_ile[j][i] > 0) { c = j + 'A'; break; } } for (int j = 0; j < n; j++) { if (--row_ile[a[j][i] - 'A'][j] == 0) { if (--row_ile_plus[j] == 1) { q.push({j, row}); } } } } else { for (int j = 0; j < 26; j++) { if (row_ile[j][i] > 0) { c = j + 'A'; break; } } for (int j = 0; j < m; j++) { if (--col_ile[a[i][j] - 'A'][j] == 0) { if (--col_ile_plus[j] == 1) { q.push({j, column}); } } } } ans.push_back({{i, type}, c}); } reverse(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (auto p : ans) { cout << (p.first.second == row ? "R" : "K") << " " << p.first.first + 1 << " " << p.second << "\n"; } } |
English