#include <iostream> #include <vector> constexpr int maxn = 2007, alfSize = 26, alfStart = 'A'; int n, m; int target[maxn][maxn]; int countRow[maxn][alfSize], countCol[maxn][alfSize]; int rowDeleted[maxn], colDeleted[maxn]; std::vector<std::pair<bool, std::pair<int, char>>> answer; void input() { scanf("%d%d", &n, &m); char c; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { scanf(" %c", &c); target[i][j] = c - alfStart; } } void count() { for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { countRow[i][target[i][j]]++; countCol[j][target[i][j]]++; } } void deleteRow(int r) { for(int i = 0; i < m; i++) { if(target[r][i] == -1) continue; countCol[i][target[r][i]]--; colDeleted[i]++; countRow[r][target[r][i]]--; rowDeleted[r]++; target[r][i] = -1; } } void deleteCol(int c) { for(int i = 0; i < n; i++) { if(target[i][c] == -1) continue; countCol[c][target[i][c]]--; colDeleted[c]++; countRow[i][target[i][c]]--; rowDeleted[i]++; target[i][c] = -1; } } bool makeStep() { // 0 - done; 1 - continue std::pair<int, std::pair<int, int>> maxRow = {0, {0, 0}}; for(int i = 0; i < n; i++) for(int colour = 0; colour < alfSize; colour++) if(rowDeleted[i] < m) maxRow = std::max(maxRow, {countRow[i][colour] + rowDeleted[i], {i, colour}}); std::pair<int, std::pair<int, int>> maxCol = {0, {0, 0}}; for(int i = 0; i < m; i++) for(int colour = 0; colour < alfSize; colour++) if(colDeleted[i] < n) maxCol = std::max(maxCol, {countCol[i][colour] + colDeleted[i], {i, colour}}); if(maxRow.first == 0 && maxCol.first == 0) return false; if(maxRow.first == m) { auto [row, colour] = maxRow.second; answer.push_back({true, {row, colour}}); deleteRow(row); return true; } auto [col, colour] = maxCol.second; answer.push_back({false, {col, colour}}); deleteCol(col); return true; } void output() { printf("%d\n", answer.size()); for(int i = answer.size() - 1; i >= 0; i--) { auto isRow = answer[i].first; auto [num, colour] = answer[i].second; printf("%c %d %c\n", isRow ? 'R' : 'K', num + 1, colour + alfStart); } } int main() { input(); count(); while(makeStep()); output(); 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 93 94 | #include <iostream> #include <vector> constexpr int maxn = 2007, alfSize = 26, alfStart = 'A'; int n, m; int target[maxn][maxn]; int countRow[maxn][alfSize], countCol[maxn][alfSize]; int rowDeleted[maxn], colDeleted[maxn]; std::vector<std::pair<bool, std::pair<int, char>>> answer; void input() { scanf("%d%d", &n, &m); char c; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { scanf(" %c", &c); target[i][j] = c - alfStart; } } void count() { for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { countRow[i][target[i][j]]++; countCol[j][target[i][j]]++; } } void deleteRow(int r) { for(int i = 0; i < m; i++) { if(target[r][i] == -1) continue; countCol[i][target[r][i]]--; colDeleted[i]++; countRow[r][target[r][i]]--; rowDeleted[r]++; target[r][i] = -1; } } void deleteCol(int c) { for(int i = 0; i < n; i++) { if(target[i][c] == -1) continue; countCol[c][target[i][c]]--; colDeleted[c]++; countRow[i][target[i][c]]--; rowDeleted[i]++; target[i][c] = -1; } } bool makeStep() { // 0 - done; 1 - continue std::pair<int, std::pair<int, int>> maxRow = {0, {0, 0}}; for(int i = 0; i < n; i++) for(int colour = 0; colour < alfSize; colour++) if(rowDeleted[i] < m) maxRow = std::max(maxRow, {countRow[i][colour] + rowDeleted[i], {i, colour}}); std::pair<int, std::pair<int, int>> maxCol = {0, {0, 0}}; for(int i = 0; i < m; i++) for(int colour = 0; colour < alfSize; colour++) if(colDeleted[i] < n) maxCol = std::max(maxCol, {countCol[i][colour] + colDeleted[i], {i, colour}}); if(maxRow.first == 0 && maxCol.first == 0) return false; if(maxRow.first == m) { auto [row, colour] = maxRow.second; answer.push_back({true, {row, colour}}); deleteRow(row); return true; } auto [col, colour] = maxCol.second; answer.push_back({false, {col, colour}}); deleteCol(col); return true; } void output() { printf("%d\n", answer.size()); for(int i = answer.size() - 1; i >= 0; i--) { auto isRow = answer[i].first; auto [num, colour] = answer[i].second; printf("%c %d %c\n", isRow ? 'R' : 'K', num + 1, colour + alfStart); } } int main() { input(); count(); while(makeStep()); output(); return 0; } |