#include <iostream> #include <set> #include <vector> #include <cassert> constexpr int max_n = 2e3 + 100; std::string color[max_n]; int no_column[max_n][26]; int no_row[max_n][26]; bool act_column[max_n]; bool act_row[max_n]; int n, m; int del_column[26]; int del_row[26]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m; std::getline(std::cin, color[0], '\n'); for (int i = 0; i < n; ++i) { std::getline(std::cin, color[i], '\n'); for (int j = 0; j < m; ++j) color[i][j] -= 'A', ++no_column[j][color[i][j]], ++no_row[i][color[i][j]]; } for (int i = 0; i < n; ++i) act_row[i] = 1; for (int i = 0; i < m; ++i) act_column[i] = 1; std::vector<char> vcr; vcr.reserve(n + m); std::vector<int> vi; vi.reserve(n + m); std::vector<char> vc; vc.reserve(n + m); int c_left = m; int r_left = n; while (c_left && r_left) { bool found = 0; for (int i = 0; i < n; ++i) { if (!act_row[i]) continue; for(int c = 0; c < 26; ++c) if(no_row[i][c] - del_row[c] == c_left) { found = 1; --r_left; act_row[i] = 0; vcr.push_back('R'); vi.push_back(i); vc.push_back(c + 'A'); ++del_column[c]; break; } } for (int i = 0; i < m; ++i) { if (!act_column[i]) continue; for(int c = 0; c < 26; ++c) if(no_column[i][c] - del_column[c] == r_left) { found = 1; --c_left; act_column[i] = 0; vcr.push_back('K'); vi.push_back(i); vc.push_back(c + 'A'); ++del_row[c]; break; } } assert(found); } std::cout << vcr.size() << '\n'; for (int i = vcr.size() - 1; i >= 0; --i) std::cout << vcr[i] << ' ' << vi[i] + 1 << ' ' << vc[i] << '\n'; }
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 | #include <iostream> #include <set> #include <vector> #include <cassert> constexpr int max_n = 2e3 + 100; std::string color[max_n]; int no_column[max_n][26]; int no_row[max_n][26]; bool act_column[max_n]; bool act_row[max_n]; int n, m; int del_column[26]; int del_row[26]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m; std::getline(std::cin, color[0], '\n'); for (int i = 0; i < n; ++i) { std::getline(std::cin, color[i], '\n'); for (int j = 0; j < m; ++j) color[i][j] -= 'A', ++no_column[j][color[i][j]], ++no_row[i][color[i][j]]; } for (int i = 0; i < n; ++i) act_row[i] = 1; for (int i = 0; i < m; ++i) act_column[i] = 1; std::vector<char> vcr; vcr.reserve(n + m); std::vector<int> vi; vi.reserve(n + m); std::vector<char> vc; vc.reserve(n + m); int c_left = m; int r_left = n; while (c_left && r_left) { bool found = 0; for (int i = 0; i < n; ++i) { if (!act_row[i]) continue; for(int c = 0; c < 26; ++c) if(no_row[i][c] - del_row[c] == c_left) { found = 1; --r_left; act_row[i] = 0; vcr.push_back('R'); vi.push_back(i); vc.push_back(c + 'A'); ++del_column[c]; break; } } for (int i = 0; i < m; ++i) { if (!act_column[i]) continue; for(int c = 0; c < 26; ++c) if(no_column[i][c] - del_column[c] == r_left) { found = 1; --c_left; act_column[i] = 0; vcr.push_back('K'); vi.push_back(i); vc.push_back(c + 'A'); ++del_row[c]; break; } } assert(found); } std::cout << vcr.size() << '\n'; for (int i = vcr.size() - 1; i >= 0; --i) std::cout << vcr[i] << ' ' << vi[i] + 1 << ' ' << vc[i] << '\n'; } |