#include <iostream> #include <unordered_map> #include <bits/stdc++.h> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int m; cin >> n; cin >> m; int total_color_count_n[n]; int total_color_count_m[m]; int duplicate_n[n]; int duplicate_m[m]; int color_freq_n[n][32]; int color_freq_m[m][32]; // Initialize all the arrays with 0 memset(total_color_count_n, 0, sizeof(total_color_count_n)); memset(total_color_count_m, 0, sizeof(total_color_count_m)); memset(color_freq_n, 0, sizeof(color_freq_n)); memset(color_freq_m, 0, sizeof(color_freq_m)); memset(duplicate_n, 0, sizeof(duplicate_n)); memset(duplicate_m, 0, sizeof(duplicate_m)); // Read data from input for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char color; cin >> color; color_freq_n[i][color - 'A']++; color_freq_m[j][color - 'A']++; } } // Intialize the total color count for (int i = 0; i < n; i++) { for (int j = 0; j < 32; j++) { if (color_freq_n[i][j] > 0) { total_color_count_n[i]++; } } } // Intialize the total color count for (int i = 0; i < m; i++) { for (int j = 0; j < 32; j++) { if (color_freq_m[i][j] > 0) { total_color_count_m[i]++; } } } std::queue<std::tuple<char, int, char>> myQueue; // Find row or with one color char type; int index; char color; // Search for rows with one color for (int i = 0; i < n; i++) { if (total_color_count_n[i] == 1) { for (int j = 0; j < 32; j++) { if (color_freq_n[i][j] > 0) { type = 'R'; index = i; color = j + 'A'; std::tuple<char, int, char> triple(type, index, color); myQueue.push(triple); duplicate_n[i] = 1; break; } } } } // If row with one color is not found, search for column with one color //if (myQueue.empty()) { for (int i = 0; i < m; i++) { if (total_color_count_m[i] == 1) { for (int j = 0; j < 32; j++) { if (color_freq_m[i][j] > 0) { type = 'K'; index = i; color = j + 'A'; std::tuple<char, int, char> triple(type, index, color); myQueue.push(triple); duplicate_m[i] = 1; break; } } } } //} vector<std::tuple<char, int, char>> solution; while (!myQueue.empty()) { // Read element from the queue std::tuple<char, int, char> element = myQueue.front(); myQueue.pop(); char type = std::get<0>(element); int index = std::get<1>(element); char color = std::get<2>(element); // Add the element to the solution solution.push_back(element); // Decrease color frequencies in appropriate row or column if (type == 'R') { total_color_count_n[index] = 0; for (int k = 0; k < 32; k++) { color_freq_n[index][k] = 0; } for (int j = 0; j < m; j++) { color_freq_m[j][color - 'A']--; if (color_freq_m[j][color - 'A'] == 0) { total_color_count_m[j]--; if (total_color_count_m[j] == 1) { for (int k = 0; k < 32; k++) { if (color_freq_m[j][k] > 0) { if (duplicate_m[j] == 0) { std::tuple<char, int, char> newTriple('K', j, k + 'A'); myQueue.push(newTriple); duplicate_m[j] = 1; break; } } } } } } } else if (type == 'K') { total_color_count_m[index] = 0; for (int k = 0; k < 32; k++) { color_freq_m[index][k] = 0; } for (int i = 0; i < n; i++) { color_freq_n[i][color - 'A']--; if (color_freq_n[i][color - 'A'] == 0) { total_color_count_n[i]--; if (total_color_count_n[i] == 1) { for (int k = 0; k < 32; k++) { if (color_freq_n[i][k] > 0) { if (duplicate_n[i] == 0) { std::tuple<char, int, char> newTriple('R', i, k + 'A'); myQueue.push(newTriple); duplicate_n[i] = 1; break; } } } } } } } } // Iterate over the solution is reverse order and print the result cout << solution.size() << endl; for (int i = solution.size() - 1; i >= 0; i--) { std::tuple<char, int, char> element = solution[i]; char type = std::get<0>(element); int index = std::get<1>(element); char color = std::get<2>(element); cout << type << " " << index + 1 << " " << color << 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include <iostream> #include <unordered_map> #include <bits/stdc++.h> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int m; cin >> n; cin >> m; int total_color_count_n[n]; int total_color_count_m[m]; int duplicate_n[n]; int duplicate_m[m]; int color_freq_n[n][32]; int color_freq_m[m][32]; // Initialize all the arrays with 0 memset(total_color_count_n, 0, sizeof(total_color_count_n)); memset(total_color_count_m, 0, sizeof(total_color_count_m)); memset(color_freq_n, 0, sizeof(color_freq_n)); memset(color_freq_m, 0, sizeof(color_freq_m)); memset(duplicate_n, 0, sizeof(duplicate_n)); memset(duplicate_m, 0, sizeof(duplicate_m)); // Read data from input for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char color; cin >> color; color_freq_n[i][color - 'A']++; color_freq_m[j][color - 'A']++; } } // Intialize the total color count for (int i = 0; i < n; i++) { for (int j = 0; j < 32; j++) { if (color_freq_n[i][j] > 0) { total_color_count_n[i]++; } } } // Intialize the total color count for (int i = 0; i < m; i++) { for (int j = 0; j < 32; j++) { if (color_freq_m[i][j] > 0) { total_color_count_m[i]++; } } } std::queue<std::tuple<char, int, char>> myQueue; // Find row or with one color char type; int index; char color; // Search for rows with one color for (int i = 0; i < n; i++) { if (total_color_count_n[i] == 1) { for (int j = 0; j < 32; j++) { if (color_freq_n[i][j] > 0) { type = 'R'; index = i; color = j + 'A'; std::tuple<char, int, char> triple(type, index, color); myQueue.push(triple); duplicate_n[i] = 1; break; } } } } // If row with one color is not found, search for column with one color //if (myQueue.empty()) { for (int i = 0; i < m; i++) { if (total_color_count_m[i] == 1) { for (int j = 0; j < 32; j++) { if (color_freq_m[i][j] > 0) { type = 'K'; index = i; color = j + 'A'; std::tuple<char, int, char> triple(type, index, color); myQueue.push(triple); duplicate_m[i] = 1; break; } } } } //} vector<std::tuple<char, int, char>> solution; while (!myQueue.empty()) { // Read element from the queue std::tuple<char, int, char> element = myQueue.front(); myQueue.pop(); char type = std::get<0>(element); int index = std::get<1>(element); char color = std::get<2>(element); // Add the element to the solution solution.push_back(element); // Decrease color frequencies in appropriate row or column if (type == 'R') { total_color_count_n[index] = 0; for (int k = 0; k < 32; k++) { color_freq_n[index][k] = 0; } for (int j = 0; j < m; j++) { color_freq_m[j][color - 'A']--; if (color_freq_m[j][color - 'A'] == 0) { total_color_count_m[j]--; if (total_color_count_m[j] == 1) { for (int k = 0; k < 32; k++) { if (color_freq_m[j][k] > 0) { if (duplicate_m[j] == 0) { std::tuple<char, int, char> newTriple('K', j, k + 'A'); myQueue.push(newTriple); duplicate_m[j] = 1; break; } } } } } } } else if (type == 'K') { total_color_count_m[index] = 0; for (int k = 0; k < 32; k++) { color_freq_m[index][k] = 0; } for (int i = 0; i < n; i++) { color_freq_n[i][color - 'A']--; if (color_freq_n[i][color - 'A'] == 0) { total_color_count_n[i]--; if (total_color_count_n[i] == 1) { for (int k = 0; k < 32; k++) { if (color_freq_n[i][k] > 0) { if (duplicate_n[i] == 0) { std::tuple<char, int, char> newTriple('R', i, k + 'A'); myQueue.push(newTriple); duplicate_n[i] = 1; break; } } } } } } } } // Iterate over the solution is reverse order and print the result cout << solution.size() << endl; for (int i = solution.size() - 1; i >= 0; i--) { std::tuple<char, int, char> element = solution[i]; char type = std::get<0>(element); int index = std::get<1>(element); char color = std::get<2>(element); cout << type << " " << index + 1 << " " << color << endl; } } |