#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
#include <ranges>
using namespace std;
int different[2][2020];
int counts[2][2020][26];
queue<pair<int, int>> q;
vector<tuple<int, int, char>> res;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
string s;
for(int i = 0; i < n; i++) {
cin >> s;
for(int j = 0; j < m; j++) {
if ((counts[0][j][s[j] - 'A']++) == 0) different[0][j]++;
if ((counts[1][i][s[j] - 'A']++) == 0) different[1][i]++;
}
}
for(int i = 0; i < n; i++) {
if (different[1][i] == 1) {
q.push({1, i});
}
}
for(int i = 0; i < m; i++) {
if (different[0][i] == 1) {
q.push({0, i});
}
}
while(!q.empty()) {
auto [t, i] = q.front();
q.pop();
int myLen = t ? m : n;
int col = -1;
for(int j = 0; j < 26; j++) {
if (counts[t][i][j]) {
col = j;
break;
}
}
if (col == -1) {
continue;
}
res.push_back({t, i, 'A' + col});
for(int j = 0; j < myLen; j++) {
if ((--counts[1 - t][j][col]) == 0) {
if ((--different[1 - t][j]) == 1) {
q.push({1 - t, j});
}
}
}
}
cout << res.size() << "\n";
for(auto [t, i, c]: ranges::reverse_view(res)) {
cout << (t?'R':'K') << " " << i + 1 << " " << c << "\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 | #include <iostream> #include <queue> #include <vector> #include <tuple> #include <ranges> using namespace std; int different[2][2020]; int counts[2][2020][26]; queue<pair<int, int>> q; vector<tuple<int, int, char>> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; string s; for(int i = 0; i < n; i++) { cin >> s; for(int j = 0; j < m; j++) { if ((counts[0][j][s[j] - 'A']++) == 0) different[0][j]++; if ((counts[1][i][s[j] - 'A']++) == 0) different[1][i]++; } } for(int i = 0; i < n; i++) { if (different[1][i] == 1) { q.push({1, i}); } } for(int i = 0; i < m; i++) { if (different[0][i] == 1) { q.push({0, i}); } } while(!q.empty()) { auto [t, i] = q.front(); q.pop(); int myLen = t ? m : n; int col = -1; for(int j = 0; j < 26; j++) { if (counts[t][i][j]) { col = j; break; } } if (col == -1) { continue; } res.push_back({t, i, 'A' + col}); for(int j = 0; j < myLen; j++) { if ((--counts[1 - t][j][col]) == 0) { if ((--different[1 - t][j]) == 1) { q.push({1 - t, j}); } } } } cout << res.size() << "\n"; for(auto [t, i, c]: ranges::reverse_view(res)) { cout << (t?'R':'K') << " " << i + 1 << " " << c << "\n"; } } |
English