#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
char tabw[n][m];
int tabr[26][n+m];
bool c[n+m];
for(int i = 0; i < n+m; i++) c[i]=0;
for(int ii=0; ii<n+m; ii++){
for(int i=0; i<26; i++){
tabr[i][ii]=0;
}
}
for(int ii=0; ii<n; ii++){
for(int i=0; i<m; i++){
char a;
cin >> a;
tabw[ii][i]=a;
tabr[(int)a-(int)'A'][ii]++;
tabr[(int)a-(int)'A'][n+i]++;
}
}
queue<int> q;
vector<pair<char, pair<int, char>>> fin;
for(int i = 0; i < n+m; i++){
int temp = 0; char t2;
for(int j = 0; j < 26; j++){
if(tabr[j][i]!=0){
temp++;
t2=(char)(j+(int)'A');
}
}
if(temp==1){
if(i>=n){
fin.push_back({'K',{i-n+1, t2}});
}else{
fin.push_back({'R',{i+1, t2}});
}
q.push(i);
c[i]=1;
}
}
while(!q.empty()){
int l = q.front();
q.pop();
if(l>=n){
for(int i = 0; i < n; i++){
if(c[i]) continue;
tabr[(int)tabw[i][l-n]-(int)'A'][i]--;
int temp = 0; char t2;
for(int j = 0; j < 26; j++){
if(tabr[j][i]!=0){
temp++;
t2=(char)(j+(int)'A');
}
}
//cout << l << ' ' << i << ' ' << temp << '\n';
if(temp==1){
fin.push_back({'R',{i+1, t2}});
q.push(i);
c[i]=1;
}
}
}else{
for(int i = n; i < n+m; i++){
if(c[i]) continue;
tabr[(int)tabw[l][i-n]-(int)'A'][i]--;
int temp = 0; char t2;
for(int j = 0; j < 26; j++){
if(tabr[j][i]!=0){
temp++;
t2=(char)(j+(int)'A');
}
}
//cout << l << ' ' << i << temp << '\n';
if(temp==1){
fin.push_back({'K',{i-n+1, t2}});
q.push(i);
c[i]=1;
}
}
}
}
cout << fin.size() << '\n';
for(int i = fin.size()-1; i >= 0; i--) cout << fin[i].first << ' ' << fin[i].second.first << ' ' << fin[i].second.second << '\n';
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; char tabw[n][m]; int tabr[26][n+m]; bool c[n+m]; for(int i = 0; i < n+m; i++) c[i]=0; for(int ii=0; ii<n+m; ii++){ for(int i=0; i<26; i++){ tabr[i][ii]=0; } } for(int ii=0; ii<n; ii++){ for(int i=0; i<m; i++){ char a; cin >> a; tabw[ii][i]=a; tabr[(int)a-(int)'A'][ii]++; tabr[(int)a-(int)'A'][n+i]++; } } queue<int> q; vector<pair<char, pair<int, char>>> fin; for(int i = 0; i < n+m; i++){ int temp = 0; char t2; for(int j = 0; j < 26; j++){ if(tabr[j][i]!=0){ temp++; t2=(char)(j+(int)'A'); } } if(temp==1){ if(i>=n){ fin.push_back({'K',{i-n+1, t2}}); }else{ fin.push_back({'R',{i+1, t2}}); } q.push(i); c[i]=1; } } while(!q.empty()){ int l = q.front(); q.pop(); if(l>=n){ for(int i = 0; i < n; i++){ if(c[i]) continue; tabr[(int)tabw[i][l-n]-(int)'A'][i]--; int temp = 0; char t2; for(int j = 0; j < 26; j++){ if(tabr[j][i]!=0){ temp++; t2=(char)(j+(int)'A'); } } //cout << l << ' ' << i << ' ' << temp << '\n'; if(temp==1){ fin.push_back({'R',{i+1, t2}}); q.push(i); c[i]=1; } } }else{ for(int i = n; i < n+m; i++){ if(c[i]) continue; tabr[(int)tabw[l][i-n]-(int)'A'][i]--; int temp = 0; char t2; for(int j = 0; j < 26; j++){ if(tabr[j][i]!=0){ temp++; t2=(char)(j+(int)'A'); } } //cout << l << ' ' << i << temp << '\n'; if(temp==1){ fin.push_back({'K',{i-n+1, t2}}); q.push(i); c[i]=1; } } } } cout << fin.size() << '\n'; for(int i = fin.size()-1; i >= 0; i--) cout << fin[i].first << ' ' << fin[i].second.first << ' ' << fin[i].second.second << '\n'; return 0; } |
English