#pragma GCC optimize("O3") #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define vv vector /*void read(int &a){ char c = getchar_unlocked(); a = 0; while(c<'0' || '9'<c) c = getchar_unlocked(); while('0'<=c&&c<='9') a = (a<<3)+(a<<1)+c-'0', c = getchar_unlocked(); }*/ int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; int add(int a, int b){return a+b >= mod ? a+b - mod : a+b;} int sub(int a, int b){return a-b < 0 ? a-b + mod : a-b;} int mul(int a, int b){return int(a * ll(b) % mod);} int fpow(int a, int b){ int ret = 1; while(b){ if(b & 1) ret = mul(ret, a); b >>= 1, a = mul(a, a); } return ret; } int inv(int a){ return fpow(a, mod-2); } int coeff(int n, int k, vector<int> &fac, vector<int> &invfac){ if(n < k) return 0; return mul(fac[n], mul(invfac[n-k], invfac[k])); } void calcfac(int n, vector<int> &fac, vector<int> &invfac){ fac[0] = 1, invfac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = mul(fac[i-1], i); invfac[n] = inv(fac[n]); for(int i = n-1; i; --i) invfac[i] = mul(invfac[i+1], i+1); } struct st{ bool col; int l; int i; //col to kolumna st(){ col = 0, l = 0, i = 0; } st(bool cc, int lll, int ii){ col = cc, l = lll, i = ii; } }; void answer(){ int n, m; scanf("%d%d", &n, &m); vv<vv<int>> t(n+1, vv<int>(m+1, 0)); char c; for(int i = 1; i <= n; ++i){ scanf("\n"); for(int j = 1; j <= m; ++j) scanf("%c", &c), t[i][j] = c-'A'; } vv<int> cntk(m+1, 0), cntr(n+1, 0), visk(m+1, 0), visr(n+1, 0); vv<vv<int>> bck(m+1, vv<int>(26, 0)), bcr(n+1, vv<int>(26, 0)); queue<st> q; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j) cntr[i] += !bcr[i][t[i][j]] ? 1 : 0, ++bcr[i][t[i][j]]; if(cntr[i] == 1){ int l = 0; for(int j = 0; j < 26; ++j) if(bcr[i][j]) l = j; q.emplace(0, l, i), visr[i] = 1; } } for(int i = 1; i <= m; ++i){ for(int j = 1; j <= n; ++j) cntk[i] += !bck[i][t[j][i]] ? 1 : 0, ++bck[i][t[j][i]]; if(cntk[i] == 1){ int l = 0; for(int j = 0; j < 26; ++j) if(bck[i][j]) l = j; q.emplace(1, l, i), visk[i] = 1; } } vv<st> out; while(!q.empty()){ st x = q.front(); q.pop(), out.emplace_back(x); //~ usleep(100000); //~ printf("%d %d %d\n", x.col, x.i, x.l); int j = x.i; if(x.col){ for(int i = 1; i <= n; ++i){ --bcr[i][t[i][j]], cntr[i] -= !bcr[i][t[i][j]] ? 1 : 0; if(cntr[i] == 1 && !visr[i]){ int l = 0; for(int jj = 0; jj < 26; ++jj) if(bcr[i][jj]) l = jj; q.emplace(0, l, i), visr[i] = 1; } } } else{ for(int i = 1; i <= m; ++i){ --bck[i][t[j][i]], cntk[i] -= !bck[i][t[j][i]] ? 1 : 0; if(cntk[i] == 1 && !visk[i]){ int l = 0; for(int jj = 0; jj < 26; ++jj) if(bck[i][jj]) l = jj; q.emplace(1, l, i), visk[i] = 1; } } } } reverse(all(out)); printf("%d\n", ssize(out)); for(st &u : out) printf("%c %d %c\n", u.col ? 'K' : 'R', u.i, u.l+'A'); } signed main(){ int T = 1; //~ scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); //cin >> T; for(++T; --T; ) answer(); 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 | #pragma GCC optimize("O3") #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define vv vector /*void read(int &a){ char c = getchar_unlocked(); a = 0; while(c<'0' || '9'<c) c = getchar_unlocked(); while('0'<=c&&c<='9') a = (a<<3)+(a<<1)+c-'0', c = getchar_unlocked(); }*/ int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; int add(int a, int b){return a+b >= mod ? a+b - mod : a+b;} int sub(int a, int b){return a-b < 0 ? a-b + mod : a-b;} int mul(int a, int b){return int(a * ll(b) % mod);} int fpow(int a, int b){ int ret = 1; while(b){ if(b & 1) ret = mul(ret, a); b >>= 1, a = mul(a, a); } return ret; } int inv(int a){ return fpow(a, mod-2); } int coeff(int n, int k, vector<int> &fac, vector<int> &invfac){ if(n < k) return 0; return mul(fac[n], mul(invfac[n-k], invfac[k])); } void calcfac(int n, vector<int> &fac, vector<int> &invfac){ fac[0] = 1, invfac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = mul(fac[i-1], i); invfac[n] = inv(fac[n]); for(int i = n-1; i; --i) invfac[i] = mul(invfac[i+1], i+1); } struct st{ bool col; int l; int i; //col to kolumna st(){ col = 0, l = 0, i = 0; } st(bool cc, int lll, int ii){ col = cc, l = lll, i = ii; } }; void answer(){ int n, m; scanf("%d%d", &n, &m); vv<vv<int>> t(n+1, vv<int>(m+1, 0)); char c; for(int i = 1; i <= n; ++i){ scanf("\n"); for(int j = 1; j <= m; ++j) scanf("%c", &c), t[i][j] = c-'A'; } vv<int> cntk(m+1, 0), cntr(n+1, 0), visk(m+1, 0), visr(n+1, 0); vv<vv<int>> bck(m+1, vv<int>(26, 0)), bcr(n+1, vv<int>(26, 0)); queue<st> q; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j) cntr[i] += !bcr[i][t[i][j]] ? 1 : 0, ++bcr[i][t[i][j]]; if(cntr[i] == 1){ int l = 0; for(int j = 0; j < 26; ++j) if(bcr[i][j]) l = j; q.emplace(0, l, i), visr[i] = 1; } } for(int i = 1; i <= m; ++i){ for(int j = 1; j <= n; ++j) cntk[i] += !bck[i][t[j][i]] ? 1 : 0, ++bck[i][t[j][i]]; if(cntk[i] == 1){ int l = 0; for(int j = 0; j < 26; ++j) if(bck[i][j]) l = j; q.emplace(1, l, i), visk[i] = 1; } } vv<st> out; while(!q.empty()){ st x = q.front(); q.pop(), out.emplace_back(x); //~ usleep(100000); //~ printf("%d %d %d\n", x.col, x.i, x.l); int j = x.i; if(x.col){ for(int i = 1; i <= n; ++i){ --bcr[i][t[i][j]], cntr[i] -= !bcr[i][t[i][j]] ? 1 : 0; if(cntr[i] == 1 && !visr[i]){ int l = 0; for(int jj = 0; jj < 26; ++jj) if(bcr[i][jj]) l = jj; q.emplace(0, l, i), visr[i] = 1; } } } else{ for(int i = 1; i <= m; ++i){ --bck[i][t[j][i]], cntk[i] -= !bck[i][t[j][i]] ? 1 : 0; if(cntk[i] == 1 && !visk[i]){ int l = 0; for(int jj = 0; jj < 26; ++jj) if(bck[i][jj]) l = jj; q.emplace(1, l, i), visk[i] = 1; } } } } reverse(all(out)); printf("%d\n", ssize(out)); for(st &u : out) printf("%c %d %c\n", u.col ? 'K' : 'R', u.i, u.l+'A'); } signed main(){ int T = 1; //~ scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); //cin >> T; for(++T; --T; ) answer(); return 0; } |