#include <iostream> #include <vector> #include <map> #include <algorithm> struct Object { int index; int i; std::vector<int> groups; }; Object buildObject(int index, std::vector<int> groups) { Object obj; obj.index = index; obj.i = index + 1; obj.groups = groups; return obj; } bool shouldProcess(Object& obj) { int i=0; for (int j=0; j<26; ++j) { if (obj.groups[j] != 0) { i++; } } return i == 1; } bool allEmpty(Object* obj) { int i=0; for (int j=0; j<26; ++j) { if (obj->groups[j] != 0) { i++; } } return i == 0; } std::vector<Object*> deleteRowOrColumn(std::vector<Object>& c, std::vector<Object>& r, int i, char charToDelete) { c.erase(c.begin() + i); for (int j = i; j < c.size(); j++) { c[j].index--; } std::vector<Object*> toProcess; for (int j = 0; j < r.size(); j++) { r[j].groups[charToDelete]--; if (r[j].groups[charToDelete] == 0 && shouldProcess(r[j])) { toProcess.push_back(&r[j]); } } return toProcess; } int getCharToDelete(Object* row) { for (int j=0; j<26; ++j) { if (row->groups[j] != 0) { return j; } } return -1; } void print(std::vector<Object>& r, std::vector<Object>& c, std::vector<Object*>& rr, std::vector<Object*>& rc) { int pchar = 'P' - 'A'; for (auto& row : r) { std::cout << "Row " << row.i << " " << row.index << " " << row.groups[0] << " " << row.groups[pchar] << std::endl; } for (auto& column : c) { std::cout << "Column " << column.i << " " << column.index << " " << column.groups[0] << " " << column.groups[pchar] << std::endl; } std::cout << "Rows to progress " << rr.size() << std::endl; for (Object* row : rr) { std::cout << "Row to progress "; std::cout << row->i << " " << row->index << " " << row->groups[0] << " " << row->groups[pchar] << std::endl; } std::cout << "Columns to progress " << rc.size() << std::endl; for (Object* column : rc) { std::cout << "Column to progress "; std::cout << " " << column->i << " " << column->index << " " << column->groups[0] << " " << column->groups[pchar] << std::endl; } std::cout << std::endl; } int main() { int n, m; std::cin >> n >> m; std::vector<std::string> rows(n); for (int i = 0; i < n; i++) { std::cin >> rows[i]; } std::vector<std::vector<int>> rowCount(n, std::vector<int>(26, 0)); // assuming ASCII characters std::vector<std::vector<int>> columnCount(m, std::vector<int>(26, 0)); // assuming ASCII characters for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rowCount[i][rows[i][j] -'A']++; columnCount[j][rows[i][j]-'A']++; } } std::vector<Object> rowObjects, columnObjects; for (int i = 0; i < n; i++) { rowObjects.push_back(buildObject(i, rowCount[i])); } for (int i = 0; i < m; i++) { columnObjects.push_back(buildObject(i, columnCount[i])); } std::vector<std::string> instructions; std::vector<Object*> rowsToProcess, columnsToProcess; for (int i=0; i< n; ++i) { if (shouldProcess(rowObjects[i])) { rowsToProcess.push_back(&rowObjects[i]); } } for (int i=0; i< m; ++i) { if (shouldProcess(columnObjects[i])) { columnsToProcess.push_back(&columnObjects[i]); } } // // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // // while (!rowsToProcess.empty() || !columnsToProcess.empty()) { std::sort(rowsToProcess.begin(), rowsToProcess.end(), [](const Object* a, const Object* b) { return a->index < b->index; // Replace 'index' with the field you want to sort by }); for (int i = rowsToProcess.size() - 1; i >= 0; i--) { // std::cout << " row 1" << std::endl; Object* row = rowsToProcess[i]; // std::cout << " row 1" << std::endl; rowsToProcess.erase(rowsToProcess.begin() + i); // std::cout << " row 1" << std::endl; if (allEmpty(row)) { break; } char charToDelete = getCharToDelete(row); char c = charToDelete + 'A'; instructions.push_back("R " + std::to_string(row->i) + " " + c); std::vector<Object*> newColumnsToProcess = deleteRowOrColumn(rowObjects, columnObjects, row->index, charToDelete); columnsToProcess.insert(columnsToProcess.end(), newColumnsToProcess.begin(), newColumnsToProcess.end()); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // std::cout << " row 1" << std::endl; } std::sort(columnsToProcess.begin(), columnsToProcess.end(), [](const Object* a, const Object* b) { return a->index < b->index; // Replace 'index' with the field you want to sort by }); for (int i = columnsToProcess.size() - 1; i >= 0; i--) { Object* column = columnsToProcess[i]; columnsToProcess.erase(columnsToProcess.begin() + i); if (allEmpty(column)) { break; } char charToDelete = getCharToDelete(column); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); char c = charToDelete + 'A'; instructions.push_back("K " + std::to_string(column->i) + " " + c); // std::cout << " delete column " << column->i << " " << column->index << std::endl; std::vector<Object*> newRowsToProcess = deleteRowOrColumn(columnObjects, rowObjects, column->index, charToDelete); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); rowsToProcess.insert(rowsToProcess.end(), newRowsToProcess.begin(), newRowsToProcess.end()); // std::cout << " column 1" << std::endl; // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // std::cout << " column 1" << std::endl; } } std::cout << instructions.size() << std::endl; for (auto it = instructions.rbegin(); it != instructions.rend(); ++it) { std::cout << *it << std::endl; } 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 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <iostream> #include <vector> #include <map> #include <algorithm> struct Object { int index; int i; std::vector<int> groups; }; Object buildObject(int index, std::vector<int> groups) { Object obj; obj.index = index; obj.i = index + 1; obj.groups = groups; return obj; } bool shouldProcess(Object& obj) { int i=0; for (int j=0; j<26; ++j) { if (obj.groups[j] != 0) { i++; } } return i == 1; } bool allEmpty(Object* obj) { int i=0; for (int j=0; j<26; ++j) { if (obj->groups[j] != 0) { i++; } } return i == 0; } std::vector<Object*> deleteRowOrColumn(std::vector<Object>& c, std::vector<Object>& r, int i, char charToDelete) { c.erase(c.begin() + i); for (int j = i; j < c.size(); j++) { c[j].index--; } std::vector<Object*> toProcess; for (int j = 0; j < r.size(); j++) { r[j].groups[charToDelete]--; if (r[j].groups[charToDelete] == 0 && shouldProcess(r[j])) { toProcess.push_back(&r[j]); } } return toProcess; } int getCharToDelete(Object* row) { for (int j=0; j<26; ++j) { if (row->groups[j] != 0) { return j; } } return -1; } void print(std::vector<Object>& r, std::vector<Object>& c, std::vector<Object*>& rr, std::vector<Object*>& rc) { int pchar = 'P' - 'A'; for (auto& row : r) { std::cout << "Row " << row.i << " " << row.index << " " << row.groups[0] << " " << row.groups[pchar] << std::endl; } for (auto& column : c) { std::cout << "Column " << column.i << " " << column.index << " " << column.groups[0] << " " << column.groups[pchar] << std::endl; } std::cout << "Rows to progress " << rr.size() << std::endl; for (Object* row : rr) { std::cout << "Row to progress "; std::cout << row->i << " " << row->index << " " << row->groups[0] << " " << row->groups[pchar] << std::endl; } std::cout << "Columns to progress " << rc.size() << std::endl; for (Object* column : rc) { std::cout << "Column to progress "; std::cout << " " << column->i << " " << column->index << " " << column->groups[0] << " " << column->groups[pchar] << std::endl; } std::cout << std::endl; } int main() { int n, m; std::cin >> n >> m; std::vector<std::string> rows(n); for (int i = 0; i < n; i++) { std::cin >> rows[i]; } std::vector<std::vector<int>> rowCount(n, std::vector<int>(26, 0)); // assuming ASCII characters std::vector<std::vector<int>> columnCount(m, std::vector<int>(26, 0)); // assuming ASCII characters for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rowCount[i][rows[i][j] -'A']++; columnCount[j][rows[i][j]-'A']++; } } std::vector<Object> rowObjects, columnObjects; for (int i = 0; i < n; i++) { rowObjects.push_back(buildObject(i, rowCount[i])); } for (int i = 0; i < m; i++) { columnObjects.push_back(buildObject(i, columnCount[i])); } std::vector<std::string> instructions; std::vector<Object*> rowsToProcess, columnsToProcess; for (int i=0; i< n; ++i) { if (shouldProcess(rowObjects[i])) { rowsToProcess.push_back(&rowObjects[i]); } } for (int i=0; i< m; ++i) { if (shouldProcess(columnObjects[i])) { columnsToProcess.push_back(&columnObjects[i]); } } // // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // // while (!rowsToProcess.empty() || !columnsToProcess.empty()) { std::sort(rowsToProcess.begin(), rowsToProcess.end(), [](const Object* a, const Object* b) { return a->index < b->index; // Replace 'index' with the field you want to sort by }); for (int i = rowsToProcess.size() - 1; i >= 0; i--) { // std::cout << " row 1" << std::endl; Object* row = rowsToProcess[i]; // std::cout << " row 1" << std::endl; rowsToProcess.erase(rowsToProcess.begin() + i); // std::cout << " row 1" << std::endl; if (allEmpty(row)) { break; } char charToDelete = getCharToDelete(row); char c = charToDelete + 'A'; instructions.push_back("R " + std::to_string(row->i) + " " + c); std::vector<Object*> newColumnsToProcess = deleteRowOrColumn(rowObjects, columnObjects, row->index, charToDelete); columnsToProcess.insert(columnsToProcess.end(), newColumnsToProcess.begin(), newColumnsToProcess.end()); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // std::cout << " row 1" << std::endl; } std::sort(columnsToProcess.begin(), columnsToProcess.end(), [](const Object* a, const Object* b) { return a->index < b->index; // Replace 'index' with the field you want to sort by }); for (int i = columnsToProcess.size() - 1; i >= 0; i--) { Object* column = columnsToProcess[i]; columnsToProcess.erase(columnsToProcess.begin() + i); if (allEmpty(column)) { break; } char charToDelete = getCharToDelete(column); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); char c = charToDelete + 'A'; instructions.push_back("K " + std::to_string(column->i) + " " + c); // std::cout << " delete column " << column->i << " " << column->index << std::endl; std::vector<Object*> newRowsToProcess = deleteRowOrColumn(columnObjects, rowObjects, column->index, charToDelete); // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); rowsToProcess.insert(rowsToProcess.end(), newRowsToProcess.begin(), newRowsToProcess.end()); // std::cout << " column 1" << std::endl; // print(rowObjects, columnObjects, rowsToProcess, columnsToProcess); // std::cout << " column 1" << std::endl; } } std::cout << instructions.size() << std::endl; for (auto it = instructions.rbegin(); it != instructions.rend(); ++it) { std::cout << *it << std::endl; } return 0; } |