#include <bits/stdc++.h> using namespace std; typedef long long LL; string s[2009]; struct history { char r; int id; char c; }; vector<history> hist; pair<int, int> r[131][2009]; pair<int, int> k[131][2009]; int ile_r[131]; int ile_k[131]; int id_r[131]; int id_k[131]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ r[(int)s[i][j]][i].first++; k[(int)s[i][j]][j].first++; r[(int)s[i][j]][i].second = i+1; k[(int)s[i][j]][j].second = j+1; } } for(int i='A'; i<='Z'; i++){ sort(r[i], r[i]+n, greater<>()); sort(k[i], k[i]+m, greater<>()); } for(int i='A'; i<='Z'; i++){ ile_r[i] = m; ile_k[i] = n; } while(n > 0 && m > 0){ bool ok = false; for(int i='A'; i<='Z'; i++){ if(ile_r[i] > 0 && ile_r[i] == r[i][id_r[i]].first){ hist.push_back({'R', r[i][id_r[i]].second, (char)i}); n--; id_r[i]++; for(int j='A'; j<='Z'; j++){ if(j!=i){ ile_k[j]--; } } ok = true; break; } } if(ok){ continue; } for(int i='A'; i<='Z'; i++){ if(ile_k[i] > 0 && ile_k[i] == k[i][id_k[i]].first){ hist.push_back({'K', k[i][id_k[i]].second, (char)i}); m--; id_k[i]++; for(int j='A'; j<='Z'; j++){ if(j!=i){ ile_r[j]--; } } break; } } } reverse(hist.begin(), hist.end()); cout << hist.size() << "\n"; for (auto [r, id, c] : hist) { cout << r << " " << id << " " << c << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; string s[2009]; struct history { char r; int id; char c; }; vector<history> hist; pair<int, int> r[131][2009]; pair<int, int> k[131][2009]; int ile_r[131]; int ile_k[131]; int id_r[131]; int id_k[131]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ r[(int)s[i][j]][i].first++; k[(int)s[i][j]][j].first++; r[(int)s[i][j]][i].second = i+1; k[(int)s[i][j]][j].second = j+1; } } for(int i='A'; i<='Z'; i++){ sort(r[i], r[i]+n, greater<>()); sort(k[i], k[i]+m, greater<>()); } for(int i='A'; i<='Z'; i++){ ile_r[i] = m; ile_k[i] = n; } while(n > 0 && m > 0){ bool ok = false; for(int i='A'; i<='Z'; i++){ if(ile_r[i] > 0 && ile_r[i] == r[i][id_r[i]].first){ hist.push_back({'R', r[i][id_r[i]].second, (char)i}); n--; id_r[i]++; for(int j='A'; j<='Z'; j++){ if(j!=i){ ile_k[j]--; } } ok = true; break; } } if(ok){ continue; } for(int i='A'; i<='Z'; i++){ if(ile_k[i] > 0 && ile_k[i] == k[i][id_k[i]].first){ hist.push_back({'K', k[i][id_k[i]].second, (char)i}); m--; id_k[i]++; for(int j='A'; j<='Z'; j++){ if(j!=i){ ile_r[j]--; } } break; } } } reverse(hist.begin(), hist.end()); cout << hist.size() << "\n"; for (auto [r, id, c] : hist) { cout << r << " " << id << " " << c << "\n"; } return 0; } |