#include <bits/stdc++.h>
#define ll long long
#define fors(u, n, s) for(ll u = (s); u < (n); u++)
#define foru(u, n) fors(u, n, 0)
#define vec vector
#define pb push_back
#define f first
#define s second
#define ir(a, b, x) (((a) <= (x)) && ((x) <= (b)))
#define pint pair<int, int>
using namespace std;
const int N = 3e3;
int n, m;
int arr[N][N];
const int C = 'Z'-'A'+1;
int rows_cnt[N][C];
int cols_cnt[N][C];
bool rows_used[N];
bool cols_used[N];
int main(){
cin >> m >> n;
foru(i, m) foru(j, n) {
char c; cin >> c;
arr[j][i] = c-'A';
}
foru(i, n) {
foru(j, m) cols_cnt[i][arr[i][j]]++;
}
foru(i, m) {
foru(j, n) rows_cnt[i][arr[j][i]]++;
}
vec<string> out;
int free_rows = m;
int free_cols = n;
while(free_cols != 0 && free_rows != 0){
start:;
if(free_cols == 0 || free_rows == 0) break;
foru(i, n){
if(cols_used[i]) continue;
int c_cnt = 0;
int color;
foru(c, C) if(cols_cnt[i][c]!=0) {c_cnt++; color=c;}
if(c_cnt == 1){
out.pb("K " + to_string(i+1) + " " + (char)('A'+color));
cols_used[i] = true;
foru(j, m) rows_cnt[j][color]--;
free_cols--;
goto start;
}
}
foru(i, m){
if(rows_used[i]) continue;
int c_cnt = 0;
int color;
foru(c, C) if(rows_cnt[i][c]!=0) {c_cnt++; color=c;}
if(c_cnt == 1){
out.pb("R " + to_string(i+1) + " " + (char)('A'+color));
rows_used[i] = true;
foru(j, n) cols_cnt[j][color]--;
free_rows--;
goto start;
}
}
}
cout << out.size() << endl;
for(int i = out.size()-1; i>=0; i--) cout << out[i] << 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 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> using namespace std; const int N = 3e3; int n, m; int arr[N][N]; const int C = 'Z'-'A'+1; int rows_cnt[N][C]; int cols_cnt[N][C]; bool rows_used[N]; bool cols_used[N]; int main(){ cin >> m >> n; foru(i, m) foru(j, n) { char c; cin >> c; arr[j][i] = c-'A'; } foru(i, n) { foru(j, m) cols_cnt[i][arr[i][j]]++; } foru(i, m) { foru(j, n) rows_cnt[i][arr[j][i]]++; } vec<string> out; int free_rows = m; int free_cols = n; while(free_cols != 0 && free_rows != 0){ start:; if(free_cols == 0 || free_rows == 0) break; foru(i, n){ if(cols_used[i]) continue; int c_cnt = 0; int color; foru(c, C) if(cols_cnt[i][c]!=0) {c_cnt++; color=c;} if(c_cnt == 1){ out.pb("K " + to_string(i+1) + " " + (char)('A'+color)); cols_used[i] = true; foru(j, m) rows_cnt[j][color]--; free_cols--; goto start; } } foru(i, m){ if(rows_used[i]) continue; int c_cnt = 0; int color; foru(c, C) if(rows_cnt[i][c]!=0) {c_cnt++; color=c;} if(c_cnt == 1){ out.pb("R " + to_string(i+1) + " " + (char)('A'+color)); rows_used[i] = true; foru(j, n) cols_cnt[j][color]--; free_rows--; goto start; } } } cout << out.size() << endl; for(int i = out.size()-1; i>=0; i--) cout << out[i] << endl; return 0; } |
English