#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX = 2000 + 10; int n, m; vec<vec<int>> row_colors(MAX, vec<int>(28)); vec<vec<int>> col_colors(MAX, vec<int>(28)); char is_single_color(vec<int> &m) { int max = 0; char max_col = 'x'; int sum = 0; for (int idx = 0; idx < 28; idx++) { if (m[idx] > max) { max = m[idx]; max_col = 'A'+idx; } sum += m[idx]; } if (sum == max) return max_col; return 'x'; } void del_perpendicular(vec<vec<int>> &v, char color) { for (auto &m: v) { m[color-'A']--; } } void solve() { vec<string> solution; rep (i, n+m) { //cout << "remove " << i << "\n"; rep(row, n) { char color = is_single_color(row_colors[row]); if (color != 'x') { string command = "R " + to_string(row+1) + " " + color; solution.eb(command); row_colors[row][color-'A'] = 0; del_perpendicular(col_colors, color); } } rep(col, m) { char color = is_single_color(col_colors[col]); if (color != 'x') { string command = "K " + to_string(col+1) + " " + color; solution.eb(command); col_colors[col][color-'A'] = 0; del_perpendicular(row_colors, color); } } } cout << solution.size() << endl; reverse(solution.begin(), solution.end()); for (string cmd: solution) { cout << cmd << endl; } } int randomNum(int a, int b) { return a + (rand() % (1 - a + b)); } bool randomBool() { return 0 + (rand() % (1 - 0 + 1)) == 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // srand(time(0)); // vec<vec<char>> tab(2000, vec<char>(2000, 'A')); // cout << "init"; // rep (_, 2000) { // char color = randomNum('B', 'Z'); // int idx = randomNum(0, 1999); // //cout << idx sp color << endl; // if (randomBool()) { // rep(row, 2000) tab[row][idx] = color; // } else { // rep(col, 2000) tab[idx][col] = color; // } // } // rep (row, 2000) { // rep (col, 2000) { // cout << tab[row][col]; // } // cout << endl; // } cin >> n >> m; rep(row, n) rep(col, m) { char c; cin >> c; row_colors[row][c-'A']++; col_colors[col][c-'A']++; } solve(); 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 | #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX = 2000 + 10; int n, m; vec<vec<int>> row_colors(MAX, vec<int>(28)); vec<vec<int>> col_colors(MAX, vec<int>(28)); char is_single_color(vec<int> &m) { int max = 0; char max_col = 'x'; int sum = 0; for (int idx = 0; idx < 28; idx++) { if (m[idx] > max) { max = m[idx]; max_col = 'A'+idx; } sum += m[idx]; } if (sum == max) return max_col; return 'x'; } void del_perpendicular(vec<vec<int>> &v, char color) { for (auto &m: v) { m[color-'A']--; } } void solve() { vec<string> solution; rep (i, n+m) { //cout << "remove " << i << "\n"; rep(row, n) { char color = is_single_color(row_colors[row]); if (color != 'x') { string command = "R " + to_string(row+1) + " " + color; solution.eb(command); row_colors[row][color-'A'] = 0; del_perpendicular(col_colors, color); } } rep(col, m) { char color = is_single_color(col_colors[col]); if (color != 'x') { string command = "K " + to_string(col+1) + " " + color; solution.eb(command); col_colors[col][color-'A'] = 0; del_perpendicular(row_colors, color); } } } cout << solution.size() << endl; reverse(solution.begin(), solution.end()); for (string cmd: solution) { cout << cmd << endl; } } int randomNum(int a, int b) { return a + (rand() % (1 - a + b)); } bool randomBool() { return 0 + (rand() % (1 - 0 + 1)) == 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // srand(time(0)); // vec<vec<char>> tab(2000, vec<char>(2000, 'A')); // cout << "init"; // rep (_, 2000) { // char color = randomNum('B', 'Z'); // int idx = randomNum(0, 1999); // //cout << idx sp color << endl; // if (randomBool()) { // rep(row, 2000) tab[row][idx] = color; // } else { // rep(col, 2000) tab[idx][col] = color; // } // } // rep (row, 2000) { // rep (col, 2000) { // cout << tab[row][col]; // } // cout << endl; // } cin >> n >> m; rep(row, n) rep(col, m) { char c; cin >> c; row_colors[row][c-'A']++; col_colors[col][c-'A']++; } solve(); return 0; } |