#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=2000; struct ope{ char t; int a; char z; }; vector<ope> ans; char arr[MAXN+2][MAXN+2]; int cnt[2*MAXN+5][28]; //rzad od [1,MAXN], kolumna [MAXN+1, 2*MAXN] int ile[2*MAXN+5]; void solve() { int n,m; cin >> n >> m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> arr[i][j]; if(!cnt[i][arr[i][j]-'A']) ile[i]++; cnt[i][arr[i][j]-'A']++; if(!cnt[j+MAXN][arr[i][j]-'A']) ile[j+MAXN]++; cnt[j+MAXN][arr[i][j]-'A']++; } } queue<int> Q; for(int i=1;i<=n;i++){ if(ile[i] == 1) Q.push(i); } for(int i=MAXN+1;i<=MAXN+m;i++){ if(ile[i] == 1) Q.push(i); } while(!Q.empty()){ int a = Q.front(); Q.pop(); if(a <= MAXN){ //rzad for(int i=0;i<26;i++){ if(cnt[a][i]){ ans.push_back({'R', a, i+'A'}); break; } } for(int i=1;i<=m;i++){ cnt[i+MAXN][arr[a][i]-'A']--; if(cnt[i+MAXN][arr[a][i]-'A'] == 0){ ile[i+MAXN]--; if(ile[i+MAXN]==1) Q.push(i+MAXN); } } } else{ //kolumna for(int i=0;i<26;i++){ if(cnt[a][i]){ ans.push_back({'K', a-MAXN, i+'A'}); break; } } for(int i=1;i<=n;i++){ cnt[i][arr[i][a-MAXN]-'A']--; if(cnt[i][arr[i][a-MAXN]-'A'] == 0){ ile[i]--; if(ile[i]==1) Q.push(i); } } } } reverse(ans.begin(), ans.end()); cout << ans.size() << "\n"; for(int i=0;i<ans.size();i++){ cout << ans[i].t << " " << ans[i].a << " " << ans[i].z << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=2000; struct ope{ char t; int a; char z; }; vector<ope> ans; char arr[MAXN+2][MAXN+2]; int cnt[2*MAXN+5][28]; //rzad od [1,MAXN], kolumna [MAXN+1, 2*MAXN] int ile[2*MAXN+5]; void solve() { int n,m; cin >> n >> m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin >> arr[i][j]; if(!cnt[i][arr[i][j]-'A']) ile[i]++; cnt[i][arr[i][j]-'A']++; if(!cnt[j+MAXN][arr[i][j]-'A']) ile[j+MAXN]++; cnt[j+MAXN][arr[i][j]-'A']++; } } queue<int> Q; for(int i=1;i<=n;i++){ if(ile[i] == 1) Q.push(i); } for(int i=MAXN+1;i<=MAXN+m;i++){ if(ile[i] == 1) Q.push(i); } while(!Q.empty()){ int a = Q.front(); Q.pop(); if(a <= MAXN){ //rzad for(int i=0;i<26;i++){ if(cnt[a][i]){ ans.push_back({'R', a, i+'A'}); break; } } for(int i=1;i<=m;i++){ cnt[i+MAXN][arr[a][i]-'A']--; if(cnt[i+MAXN][arr[a][i]-'A'] == 0){ ile[i+MAXN]--; if(ile[i+MAXN]==1) Q.push(i+MAXN); } } } else{ //kolumna for(int i=0;i<26;i++){ if(cnt[a][i]){ ans.push_back({'K', a-MAXN, i+'A'}); break; } } for(int i=1;i<=n;i++){ cnt[i][arr[i][a-MAXN]-'A']--; if(cnt[i][arr[i][a-MAXN]-'A'] == 0){ ile[i]--; if(ile[i]==1) Q.push(i); } } } } reverse(ans.begin(), ans.end()); cout << ans.size() << "\n"; for(int i=0;i<ans.size();i++){ cout << ans[i].t << " " << ans[i].a << " " << ans[i].z << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |