#include <bits/stdc++.h> using namespace std; int n, m; unordered_map<char, int> r[2003], k[2003]; struct A { char type; int nr; char color; A(char t, int num, char col) { type = t; nr = num; color = col; } }; vector<A> result; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; string str; for(int i=0;i<n;i++) { cin >> str; for(int j=0;j<m;j++) { char ch = str[j]; r[i][ch]++; k[j][ch]++; } } unordered_set<int> candR, candK; for(int i=0;i<n;i++) { if(r[i].size()<=1) { candR.insert(i); } } for(int j=0;j<m;j++) { if(k[j].size()<=1) { candK.insert(j); } } set<int> processedK, processedR; while(candR.size() > 0 || candK.size() > 0) { if(candR.size() > 0) { int i = *candR.begin(); candR.erase(i); processedR.insert(i); if(r[i].size() == 1) { char col = r[i].begin()->first; result.push_back(A('R', i, col)); for(int j=0;j<m;j++) { k[j][col]--; if(k[j][col] == 0) { k[j].erase(col); if(k[j].size() == 1 && processedK.find(j) == processedK.end()) { candK.insert(j); } } } } } else { int j = *candK.begin(); candK.erase(j); processedK.insert(j); if(k[j].size() == 1) { char col = k[j].begin()->first; result.push_back(A('K', j, col)); for(int i=0;i<n;i++) { r[i][col]--; if(r[i][col] == 0) { r[i].erase(col); if(r[i].size() == 1 && processedR.find(i) == processedR.end()) { candR.insert(i); } } } } } } cout << result.size() << endl; for(int i=result.size()-1;i>=0;i--) { cout << result[i].type << " " << (result[i].nr+1) << " " << result[i].color << endl; } 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 | #include <bits/stdc++.h> using namespace std; int n, m; unordered_map<char, int> r[2003], k[2003]; struct A { char type; int nr; char color; A(char t, int num, char col) { type = t; nr = num; color = col; } }; vector<A> result; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; string str; for(int i=0;i<n;i++) { cin >> str; for(int j=0;j<m;j++) { char ch = str[j]; r[i][ch]++; k[j][ch]++; } } unordered_set<int> candR, candK; for(int i=0;i<n;i++) { if(r[i].size()<=1) { candR.insert(i); } } for(int j=0;j<m;j++) { if(k[j].size()<=1) { candK.insert(j); } } set<int> processedK, processedR; while(candR.size() > 0 || candK.size() > 0) { if(candR.size() > 0) { int i = *candR.begin(); candR.erase(i); processedR.insert(i); if(r[i].size() == 1) { char col = r[i].begin()->first; result.push_back(A('R', i, col)); for(int j=0;j<m;j++) { k[j][col]--; if(k[j][col] == 0) { k[j].erase(col); if(k[j].size() == 1 && processedK.find(j) == processedK.end()) { candK.insert(j); } } } } } else { int j = *candK.begin(); candK.erase(j); processedK.insert(j); if(k[j].size() == 1) { char col = k[j].begin()->first; result.push_back(A('K', j, col)); for(int i=0;i<n;i++) { r[i][col]--; if(r[i][col] == 0) { r[i].erase(col); if(r[i].size() == 1 && processedR.find(i) == processedR.end()) { candR.insert(i); } } } } } } cout << result.size() << endl; for(int i=result.size()-1;i>=0;i--) { cout << result[i].type << " " << (result[i].nr+1) << " " << result[i].color << endl; } return 0; } |