#include<bits/stdc++.h> using namespace std; const int N = 2000 + 5; class info{ public: int p, x; inline info() {} inline info(int p0, int x0){ p = p0, x = x0; } }; int n = 0, m = 0; int r[N][30] = {}, k[N][30] = {}, R[N] = {}, K[N] = {}; char str[N] = {}; queue<info> Q; inline void work(){ if(!Q.size()) return; info bag = Q.front(); Q.pop(); if(bag.p){ int j = bag.x, w = -1; for(int c = 0 ; c < 26 ; c ++) if(k[j][c]) w = c; for(int i = 0 ; i < n ; i ++) if(R[i] > 1){ r[i][w] --; if(!r[i][w]){ R[i] --; if(R[i] == 1) Q.push(info(0, i)); } } work(); printf("K %d %c\n", j + 1, char('A' + w)); } else{ int i = bag.x, w = -1; for(int c = 0 ; c < 26 ; c ++) if(r[i][c]) w = c; for(int j = 0 ; j < m ; j ++) if(K[j] > 1){ k[j][w] --; if(!k[j][w]){ K[j] --; if(K[j] == 1) Q.push(info(1, j)); } } work(); printf("R %d %c\n", i + 1, char('A' + w)); } } int main(){ scanf("%d %d", &n, &m); for(int i = 0 ; i < n ; i ++){ scanf("%s", str); for(int j = 0 ; j < m ; j ++){ int c = str[j] - 'A'; r[i][c] ++, k[j][c] ++; } } for(int i = 0 ; i < n ; i ++) for(int c = 0 ; c < 26 ; c ++) if(r[i][c]) R[i] ++; for(int j = 0 ; j < m ; j ++) for(int c = 0 ; c < 26 ; c ++) if(k[j][c]) K[j] ++; for(int i = 0 ; i < n ; i ++) if(R[i] == 1) Q.push(info(0, i)); for(int j = 0 ; j < m ; j ++) if(K[j] == 1) Q.push(info(1, j)); printf("%d\n", n + m); work(); 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 | #include<bits/stdc++.h> using namespace std; const int N = 2000 + 5; class info{ public: int p, x; inline info() {} inline info(int p0, int x0){ p = p0, x = x0; } }; int n = 0, m = 0; int r[N][30] = {}, k[N][30] = {}, R[N] = {}, K[N] = {}; char str[N] = {}; queue<info> Q; inline void work(){ if(!Q.size()) return; info bag = Q.front(); Q.pop(); if(bag.p){ int j = bag.x, w = -1; for(int c = 0 ; c < 26 ; c ++) if(k[j][c]) w = c; for(int i = 0 ; i < n ; i ++) if(R[i] > 1){ r[i][w] --; if(!r[i][w]){ R[i] --; if(R[i] == 1) Q.push(info(0, i)); } } work(); printf("K %d %c\n", j + 1, char('A' + w)); } else{ int i = bag.x, w = -1; for(int c = 0 ; c < 26 ; c ++) if(r[i][c]) w = c; for(int j = 0 ; j < m ; j ++) if(K[j] > 1){ k[j][w] --; if(!k[j][w]){ K[j] --; if(K[j] == 1) Q.push(info(1, j)); } } work(); printf("R %d %c\n", i + 1, char('A' + w)); } } int main(){ scanf("%d %d", &n, &m); for(int i = 0 ; i < n ; i ++){ scanf("%s", str); for(int j = 0 ; j < m ; j ++){ int c = str[j] - 'A'; r[i][c] ++, k[j][c] ++; } } for(int i = 0 ; i < n ; i ++) for(int c = 0 ; c < 26 ; c ++) if(r[i][c]) R[i] ++; for(int j = 0 ; j < m ; j ++) for(int c = 0 ; c < 26 ; c ++) if(k[j][c]) K[j] ++; for(int i = 0 ; i < n ; i ++) if(R[i] == 1) Q.push(info(0, i)); for(int j = 0 ; j < m ; j ++) if(K[j] == 1) Q.push(info(1, j)); printf("%d\n", n + m); work(); return 0; } |