#include <bits/stdc++.h> using namespace std; constexpr int maxN = 2e3, maxM = 2e3; constexpr int ALPH = 26; int n, m; char c[maxN + 1][maxM + 1]; int cnt_row[maxN + 1][ALPH + 1]; bool used_row[maxN + 1]; int cnt_col[maxM + 1][ALPH + 1]; bool used_col[maxM + 1]; vector<pair<char, pair<int, char>>> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { cin >> c[i][j]; cnt_row[i][c[i][j] - 'A' + 1]++; cnt_col[j][c[i][j] - 'A' + 1]++; } } for(int iter=1; iter<=n+m; iter++) { for(int i=1; i<=n; i++) { if(!used_row[i]) { int cnt_max = cnt_row[i][1]; int cnt_id = 1; for(int id=2; id<=ALPH; id++) { if(cnt_max < cnt_row[i][id]) { cnt_max = cnt_row[i][id]; cnt_id = id; } } if(cnt_max + cnt_row[i][0] == m) { used_row[i] = true; ans.push_back({'R', {i, cnt_id + 'A' - 1}}); for(int j=1; j<=m; j++) { cnt_row[i][c[i][j] - 'A' + 1]--; cnt_col[j][c[i][j] - 'A' + 1]--; c[i][j] = 'A'; cnt_row[i][0]++; cnt_col[j][0]++; } goto skip; } } } for(int j=1; j<=m; j++) { if(!used_col[j]) { int cnt_max = cnt_col[j][1]; int cnt_id = 1; for(int id=2; id<=ALPH; id++) { if(cnt_max < cnt_col[j][id]) { cnt_max = cnt_col[j][id]; cnt_id = id; } } if(cnt_max + cnt_col[j][0] == n) { used_col[j] = true; ans.push_back({'K', {j, cnt_id + 'A' - 1}}); for(int i=1; i<=n; i++) { cnt_row[i][c[i][j] - 'A' + 1]--; cnt_col[j][c[i][j] - 'A' + 1]--; c[i][j] = 'A'; cnt_row[i][0]++; cnt_col[j][0]++; } goto skip; } } } skip:; } reverse(ans.begin(), ans.end()); cout << ans.size() << '\n'; for(pair<char, pair<int, char>> i : ans) { cout << i.first << ' ' << i.second.first << ' ' << i.second.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 102 103 104 105 106 107 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 2e3, maxM = 2e3; constexpr int ALPH = 26; int n, m; char c[maxN + 1][maxM + 1]; int cnt_row[maxN + 1][ALPH + 1]; bool used_row[maxN + 1]; int cnt_col[maxM + 1][ALPH + 1]; bool used_col[maxM + 1]; vector<pair<char, pair<int, char>>> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { cin >> c[i][j]; cnt_row[i][c[i][j] - 'A' + 1]++; cnt_col[j][c[i][j] - 'A' + 1]++; } } for(int iter=1; iter<=n+m; iter++) { for(int i=1; i<=n; i++) { if(!used_row[i]) { int cnt_max = cnt_row[i][1]; int cnt_id = 1; for(int id=2; id<=ALPH; id++) { if(cnt_max < cnt_row[i][id]) { cnt_max = cnt_row[i][id]; cnt_id = id; } } if(cnt_max + cnt_row[i][0] == m) { used_row[i] = true; ans.push_back({'R', {i, cnt_id + 'A' - 1}}); for(int j=1; j<=m; j++) { cnt_row[i][c[i][j] - 'A' + 1]--; cnt_col[j][c[i][j] - 'A' + 1]--; c[i][j] = 'A'; cnt_row[i][0]++; cnt_col[j][0]++; } goto skip; } } } for(int j=1; j<=m; j++) { if(!used_col[j]) { int cnt_max = cnt_col[j][1]; int cnt_id = 1; for(int id=2; id<=ALPH; id++) { if(cnt_max < cnt_col[j][id]) { cnt_max = cnt_col[j][id]; cnt_id = id; } } if(cnt_max + cnt_col[j][0] == n) { used_col[j] = true; ans.push_back({'K', {j, cnt_id + 'A' - 1}}); for(int i=1; i<=n; i++) { cnt_row[i][c[i][j] - 'A' + 1]--; cnt_col[j][c[i][j] - 'A' + 1]--; c[i][j] = 'A'; cnt_row[i][0]++; cnt_col[j][0]++; } goto skip; } } } skip:; } reverse(ans.begin(), ans.end()); cout << ans.size() << '\n'; for(pair<char, pair<int, char>> i : ans) { cout << i.first << ' ' << i.second.first << ' ' << i.second.second << '\n'; } } |