#include <iostream> #include <vector> #include <sstream> #include <string> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> board; vector<vector<int>> count_row; vector<vector<int>> count_col; board.resize(n, vector<int>(m)); count_row.resize(n, vector<int>(28)); count_col.resize(m, vector<int>(28)); string line; vector<string> operations; stringstream output; for (int i = 0; i < n; i++) { cin >> line; for (int j = 0; j < line.size(); j++) { board[i][j] = (int)line[j] - 64; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { count_row[i][board[i][j]]++; count_col[j][board[i][j]]++; } } for (int i = 0; i < n; i++) { int nonzero = 0; for (int l = 0; l < 27; l++) { if (count_row[i][l] > 0) { nonzero++; } } count_row[i][27] = nonzero; } for (int j = 0; j < m; j++) { int nonzero = 0; for (int l = 0; l < 27; l++) { if (count_col[j][l] > 0) { nonzero++; } } count_col[j][27] = nonzero; } //for (int i = 0; i < n; i++) { // for (int l = 0; l < 28; l++) { // cout << count_row[i][l] << " "; // } // cout << endl; //} //cout << endl; //for (int j = 0; j < m; j++) { // for (int l = 0; l < 28; l++) { // cout << count_col[j][l] << " "; // } // cout << endl; //} int s = n + m; while (--s >= 0) { int candidate = -1; char candidate_type = 'N'; int letter = -1; for (int i = 0; i < n; i++) { if (count_row[i][27] == 1) { candidate = i; candidate_type = 'R'; for (int l = 0; l < 27; l++) { if (count_row[i][l] > 0) { letter = l; break; } } break; } } if (candidate_type == 'N') { for (int i = 0; i < m; i++) { if (count_col[i][27] == 1) { candidate = i; candidate_type = 'C'; for (int l = 0; l < 27; l++) { if (count_col[i][l] > 0) { letter = l; break; } } break; } } } if (candidate_type == 'R') { // remove row count_row[candidate][27] = 0; // update letter count in every column for (int j = 0; j < m; j++) { count_col[j][letter]--; // update non zero count if letter count drops to 0 if (count_col[j][letter] == 0) { count_col[j][27]--; } } operations.push_back("R " + to_string(candidate + 1) + " " + static_cast<char>(letter + 64)); } else if (candidate_type == 'C') { // remove col count_col[candidate][27] = 0; // update letter count in every column for (int j = 0; j < n; j++) { count_row[j][letter]--; // update non zero count if letter count drops to 0 if (count_row[j][letter] == 0) { count_row[j][27]--; } } operations.push_back("K " + to_string(candidate + 1) + " " + static_cast<char>(letter + 64)); } else { } }; cout << operations.size() << endl; for (int i = operations.size() - 1; i >= 0; i--) { cout << operations[i] << endl; } cout << endl; }
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include <iostream> #include <vector> #include <sstream> #include <string> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> board; vector<vector<int>> count_row; vector<vector<int>> count_col; board.resize(n, vector<int>(m)); count_row.resize(n, vector<int>(28)); count_col.resize(m, vector<int>(28)); string line; vector<string> operations; stringstream output; for (int i = 0; i < n; i++) { cin >> line; for (int j = 0; j < line.size(); j++) { board[i][j] = (int)line[j] - 64; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { count_row[i][board[i][j]]++; count_col[j][board[i][j]]++; } } for (int i = 0; i < n; i++) { int nonzero = 0; for (int l = 0; l < 27; l++) { if (count_row[i][l] > 0) { nonzero++; } } count_row[i][27] = nonzero; } for (int j = 0; j < m; j++) { int nonzero = 0; for (int l = 0; l < 27; l++) { if (count_col[j][l] > 0) { nonzero++; } } count_col[j][27] = nonzero; } //for (int i = 0; i < n; i++) { // for (int l = 0; l < 28; l++) { // cout << count_row[i][l] << " "; // } // cout << endl; //} //cout << endl; //for (int j = 0; j < m; j++) { // for (int l = 0; l < 28; l++) { // cout << count_col[j][l] << " "; // } // cout << endl; //} int s = n + m; while (--s >= 0) { int candidate = -1; char candidate_type = 'N'; int letter = -1; for (int i = 0; i < n; i++) { if (count_row[i][27] == 1) { candidate = i; candidate_type = 'R'; for (int l = 0; l < 27; l++) { if (count_row[i][l] > 0) { letter = l; break; } } break; } } if (candidate_type == 'N') { for (int i = 0; i < m; i++) { if (count_col[i][27] == 1) { candidate = i; candidate_type = 'C'; for (int l = 0; l < 27; l++) { if (count_col[i][l] > 0) { letter = l; break; } } break; } } } if (candidate_type == 'R') { // remove row count_row[candidate][27] = 0; // update letter count in every column for (int j = 0; j < m; j++) { count_col[j][letter]--; // update non zero count if letter count drops to 0 if (count_col[j][letter] == 0) { count_col[j][27]--; } } operations.push_back("R " + to_string(candidate + 1) + " " + static_cast<char>(letter + 64)); } else if (candidate_type == 'C') { // remove col count_col[candidate][27] = 0; // update letter count in every column for (int j = 0; j < n; j++) { count_row[j][letter]--; // update non zero count if letter count drops to 0 if (count_row[j][letter] == 0) { count_row[j][27]--; } } operations.push_back("K " + to_string(candidate + 1) + " " + static_cast<char>(letter + 64)); } else { } }; cout << operations.size() << endl; for (int i = operations.size() - 1; i >= 0; i--) { cout << operations[i] << endl; } cout << endl; } |