#include <iostream> #include <vector> #include <unordered_set> struct Move { bool is_col; int ind; char color; Move(bool is_col, int ind, int color) : is_col(is_col), ind(ind), color((char)color + 'A') {} }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector<std::vector<int>> c_color(m, std::vector<int>(26)), r_color(n, std::vector<int>(26)); for (int i = 0; i < n; ++i) { std::string row; std::cin >> row; for (int j = 0; j < m; ++j) { int ind = row[j] - 'A'; ++c_color[j][ind]; ++r_color[i][ind]; } } std::vector<std::unordered_set<int>> col_color_count(n + 1); std::vector<std::unordered_set<int>> row_color_count(m + 1); for (int i = 0; i < n; ++i) { for (int c = 0; c < 26; ++c) { if (r_color[i][c]) { row_color_count[r_color[i][c]].insert(26 * i + c); } } } for (int i = 0; i < m; ++i) { for (int c = 0; c < 26; ++c) { if (c_color[i][c]) { col_color_count[c_color[i][c]].insert(26 * i + c); } } } int col_size = n; int row_size = m; std::vector<bool> col_alive(m, true), row_alive(n, true); std::vector<Move> moves; while (col_size > 0 && row_size > 0) { for (int comb : row_color_count[row_size]) { int i = comb / 26; int c = comb % 26; if (!row_alive[i]) continue; moves.emplace_back(false, i, c); row_alive[i] = false; --col_size; for (int j = 0; j < m; ++j) { if (int count = c_color[j][c]) { --c_color[j][c]; col_color_count[count].erase(j * 26 + c); if (--count) { col_color_count[count].insert(j * 26 + c); } } } } if (!col_size) break; for (int comb : col_color_count[col_size]) { int i = comb / 26; int c = comb % 26; if (!col_alive[i]) continue; moves.emplace_back(true, i, c); col_alive[i] = false; --row_size; for (int j = 0; j < n; ++j) { if (int count = r_color[j][c]) { --r_color[j][c]; row_color_count[count].erase(j * 26 + c); if (--count) { row_color_count[count].insert(j * 26 + c); } } } } } std::cout << moves.size() << std::endl; for (int i = moves.size() - 1; i >= 0; --i) { std::cout << (moves[i].is_col ? "K " : "R ") << moves[i].ind + 1 << " " << moves[i].color << 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 | #include <iostream> #include <vector> #include <unordered_set> struct Move { bool is_col; int ind; char color; Move(bool is_col, int ind, int color) : is_col(is_col), ind(ind), color((char)color + 'A') {} }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector<std::vector<int>> c_color(m, std::vector<int>(26)), r_color(n, std::vector<int>(26)); for (int i = 0; i < n; ++i) { std::string row; std::cin >> row; for (int j = 0; j < m; ++j) { int ind = row[j] - 'A'; ++c_color[j][ind]; ++r_color[i][ind]; } } std::vector<std::unordered_set<int>> col_color_count(n + 1); std::vector<std::unordered_set<int>> row_color_count(m + 1); for (int i = 0; i < n; ++i) { for (int c = 0; c < 26; ++c) { if (r_color[i][c]) { row_color_count[r_color[i][c]].insert(26 * i + c); } } } for (int i = 0; i < m; ++i) { for (int c = 0; c < 26; ++c) { if (c_color[i][c]) { col_color_count[c_color[i][c]].insert(26 * i + c); } } } int col_size = n; int row_size = m; std::vector<bool> col_alive(m, true), row_alive(n, true); std::vector<Move> moves; while (col_size > 0 && row_size > 0) { for (int comb : row_color_count[row_size]) { int i = comb / 26; int c = comb % 26; if (!row_alive[i]) continue; moves.emplace_back(false, i, c); row_alive[i] = false; --col_size; for (int j = 0; j < m; ++j) { if (int count = c_color[j][c]) { --c_color[j][c]; col_color_count[count].erase(j * 26 + c); if (--count) { col_color_count[count].insert(j * 26 + c); } } } } if (!col_size) break; for (int comb : col_color_count[col_size]) { int i = comb / 26; int c = comb % 26; if (!col_alive[i]) continue; moves.emplace_back(true, i, c); col_alive[i] = false; --row_size; for (int j = 0; j < n; ++j) { if (int count = r_color[j][c]) { --r_color[j][c]; row_color_count[count].erase(j * 26 + c); if (--count) { row_color_count[count].insert(j * 26 + c); } } } } } std::cout << moves.size() << std::endl; for (int i = moves.size() - 1; i >= 0; --i) { std::cout << (moves[i].is_col ? "K " : "R ") << moves[i].ind + 1 << " " << moves[i].color << std::endl; } return 0; } |