// PA 2024 4C - Lamiglowka 3 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> using namespace std; struct Result { char rowcol; int nr; char color; Result(char rowcol, int nr, char color) { this->rowcol = rowcol; this->nr = nr; this->color = color; } }; vector<vector<int>> rowCounter; vector<vector<int>> columnCounter; int rowDeletedInfo[26]; int columnDeletedInfo[26]; vector<int> rowMarked; vector<int> columnMarked; stack<Result> results; int n, m, currN, currM; bool rowFound() { for (int i = 0; i < n; ++i) { for (int j = 0; j < 26; ++j) { if (rowMarked[i] == false && rowCounter[i][j] - rowDeletedInfo[j] == currM) { results.push(Result('R', i, j + 'A')); --currN; rowMarked[i] = true; ++columnDeletedInfo[j]; return true; } } } return false; } bool columnFound() { for (int i = 0; i < m; ++i) { for (int j = 0; j < 26; ++j) { if (columnMarked[i] == false && columnCounter[i][j] - columnDeletedInfo[j] == currN) { results.push(Result('K', i, j + 'A')); --currM; columnMarked[i] = true; ++rowDeletedInfo[j]; return true; } } } return false; } int main() { ios_base::sync_with_stdio(0); char c; cin >> n; cin >> m; for (int i = 0; i < n; ++i) { rowCounter.push_back(vector<int>()); rowMarked.push_back(false); for (int j = 0; j < 26; ++j) { rowCounter[i].push_back(0); } } for (int i = 0; i < m; ++i) { columnCounter.push_back(vector<int>()); columnMarked.push_back(false); for (int j = 0; j < 26; ++j) { columnCounter[i].push_back(0); } } for (int i = 0; i < 26; ++i) { rowDeletedInfo[i] = 0; columnDeletedInfo[i] = 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> c; ++rowCounter[i][c - 'A']; ++columnCounter[j][c - 'A']; } } currN = n; currM = m; while (currN > 0 && currM > 0) { if (not rowFound()) { columnFound(); } } cout << results.size() << endl; while (not results.empty()) { cout << results.top().rowcol << " " << results.top().nr + 1 << " " << results.top().color << endl; results.pop(); } 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 | // PA 2024 4C - Lamiglowka 3 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> using namespace std; struct Result { char rowcol; int nr; char color; Result(char rowcol, int nr, char color) { this->rowcol = rowcol; this->nr = nr; this->color = color; } }; vector<vector<int>> rowCounter; vector<vector<int>> columnCounter; int rowDeletedInfo[26]; int columnDeletedInfo[26]; vector<int> rowMarked; vector<int> columnMarked; stack<Result> results; int n, m, currN, currM; bool rowFound() { for (int i = 0; i < n; ++i) { for (int j = 0; j < 26; ++j) { if (rowMarked[i] == false && rowCounter[i][j] - rowDeletedInfo[j] == currM) { results.push(Result('R', i, j + 'A')); --currN; rowMarked[i] = true; ++columnDeletedInfo[j]; return true; } } } return false; } bool columnFound() { for (int i = 0; i < m; ++i) { for (int j = 0; j < 26; ++j) { if (columnMarked[i] == false && columnCounter[i][j] - columnDeletedInfo[j] == currN) { results.push(Result('K', i, j + 'A')); --currM; columnMarked[i] = true; ++rowDeletedInfo[j]; return true; } } } return false; } int main() { ios_base::sync_with_stdio(0); char c; cin >> n; cin >> m; for (int i = 0; i < n; ++i) { rowCounter.push_back(vector<int>()); rowMarked.push_back(false); for (int j = 0; j < 26; ++j) { rowCounter[i].push_back(0); } } for (int i = 0; i < m; ++i) { columnCounter.push_back(vector<int>()); columnMarked.push_back(false); for (int j = 0; j < 26; ++j) { columnCounter[i].push_back(0); } } for (int i = 0; i < 26; ++i) { rowDeletedInfo[i] = 0; columnDeletedInfo[i] = 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> c; ++rowCounter[i][c - 'A']; ++columnCounter[j][c - 'A']; } } currN = n; currM = m; while (currN > 0 && currM > 0) { if (not rowFound()) { columnFound(); } } cout << results.size() << endl; while (not results.empty()) { cout << results.top().rowcol << " " << results.top().nr + 1 << " " << results.top().color << endl; results.pop(); } return 0; } |