#include "bits/stdc++.h" using namespace std; struct wynik{ char typ; int indeks; int wartosc; void display(){ char tmp = 'A' + wartosc; cout << typ << ' ' << indeks + 1 << ' ' << tmp << '\n'; } wynik (char arg1, int arg2, int arg3) { typ = arg1; indeks = arg2; wartosc = arg3; } }; vector<wynik> res; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<array<int, 26>> rows(n); vector<bool> active_rows(n, 1); int num_of_inactive_rows=0; vector<array<int, 26>> columns(m); vector<bool> active_columns(m, 1); int num_of_inactive_columns=0; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { char x; cin >> x; x -= 'A'; rows[i][x]++; columns[j][x]++; } int counter = n + m; while(counter--) { for(int i=0; i<n; i++){ if(!active_rows[i]) continue; for(int let=0; let<26; let++){ if(rows[i][let] == m - num_of_inactive_columns) { active_rows[i] = false; num_of_inactive_rows++; res.push_back({'R', i, let}); for(int j=0; j<m; j++) if(active_columns[j]){ rows[i][let]--; columns[j][let]--; } break; } } } for(int i=0; i<m; i++){ if(!active_columns[i]) continue; for(int let=0; let<26; let++){ if(columns[i][let] == n - num_of_inactive_rows) { active_columns[i] = false; num_of_inactive_columns++; res.push_back({'K', i, let}); for(int j=0; j<n; j++) if(active_rows[j]){ columns[i][let]--; rows[j][let]--; } break; } } } } cout << res.size() << '\n'; for(auto r : res | views::reverse) r.display(); 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; struct wynik{ char typ; int indeks; int wartosc; void display(){ char tmp = 'A' + wartosc; cout << typ << ' ' << indeks + 1 << ' ' << tmp << '\n'; } wynik (char arg1, int arg2, int arg3) { typ = arg1; indeks = arg2; wartosc = arg3; } }; vector<wynik> res; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<array<int, 26>> rows(n); vector<bool> active_rows(n, 1); int num_of_inactive_rows=0; vector<array<int, 26>> columns(m); vector<bool> active_columns(m, 1); int num_of_inactive_columns=0; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { char x; cin >> x; x -= 'A'; rows[i][x]++; columns[j][x]++; } int counter = n + m; while(counter--) { for(int i=0; i<n; i++){ if(!active_rows[i]) continue; for(int let=0; let<26; let++){ if(rows[i][let] == m - num_of_inactive_columns) { active_rows[i] = false; num_of_inactive_rows++; res.push_back({'R', i, let}); for(int j=0; j<m; j++) if(active_columns[j]){ rows[i][let]--; columns[j][let]--; } break; } } } for(int i=0; i<m; i++){ if(!active_columns[i]) continue; for(int let=0; let<26; let++){ if(columns[i][let] == n - num_of_inactive_rows) { active_columns[i] = false; num_of_inactive_columns++; res.push_back({'K', i, let}); for(int j=0; j<n; j++) if(active_rows[j]){ columns[i][let]--; rows[j][let]--; } break; } } } } cout << res.size() << '\n'; for(auto r : res | views::reverse) r.display(); return 0; } |