//Jakub Trela
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct Odp{
char typ;
int x;
char k;
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
vector<vector<int>> wiersze(n,vector<int>(26));
vector<vector<int>> kolumny(m,vector<int>(26));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
char z;
cin>>z;
wiersze[i][z-'A']++;
kolumny[j][z-'A']++;
}
}
int r=0;
stack<Odp> ans;
while(++r){
bool czyPusta=1;
bool kont=0;
for(int i=0;i<n;i++){
int licz=0;
char kolor;
for(int c=0;c<26;c++){
if(wiersze[i][c]>0){
kolor=c+'A';
licz++;
czyPusta=0;
}
}
if(licz==1){
for(int c=0;c<26;c++){
wiersze[i][c]=0;
}
for(int j=0;j<m;j++){
kolumny[j][kolor-'A']--;
}
ans.push({'R',i+1,kolor});
kont=1;
break;
}
}
if(kont)continue;
for(int j=0;j<m;j++){
int licz=0;
char kolor;
for(int c=0;c<26;c++){
if(kolumny[j][c]>0){
kolor=c+'A';
licz++;
czyPusta=0;
}
}
if(licz==1){
for(int c=0;c<26;c++){
kolumny[j][c]=0;
}
for(int i=0;i<n;i++){
wiersze[i][kolor-'A']--;
}
ans.push({'K',j+1,kolor});
kont=1;
break;
}
}
if(kont)continue;
if(czyPusta)break;
}
cout<<r-1<<endl;
while(!ans.empty()){
cout<<ans.top().typ<<" "<<ans.top().x<<" "<<ans.top().k<<endl;
ans.pop();
}
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 | //Jakub Trela #include <bits/stdc++.h> using namespace std; typedef long long LL; struct Odp{ char typ; int x; char k; }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; vector<vector<int>> wiersze(n,vector<int>(26)); vector<vector<int>> kolumny(m,vector<int>(26)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ char z; cin>>z; wiersze[i][z-'A']++; kolumny[j][z-'A']++; } } int r=0; stack<Odp> ans; while(++r){ bool czyPusta=1; bool kont=0; for(int i=0;i<n;i++){ int licz=0; char kolor; for(int c=0;c<26;c++){ if(wiersze[i][c]>0){ kolor=c+'A'; licz++; czyPusta=0; } } if(licz==1){ for(int c=0;c<26;c++){ wiersze[i][c]=0; } for(int j=0;j<m;j++){ kolumny[j][kolor-'A']--; } ans.push({'R',i+1,kolor}); kont=1; break; } } if(kont)continue; for(int j=0;j<m;j++){ int licz=0; char kolor; for(int c=0;c<26;c++){ if(kolumny[j][c]>0){ kolor=c+'A'; licz++; czyPusta=0; } } if(licz==1){ for(int c=0;c<26;c++){ kolumny[j][c]=0; } for(int i=0;i<n;i++){ wiersze[i][kolor-'A']--; } ans.push({'K',j+1,kolor}); kont=1; break; } } if(kont)continue; if(czyPusta)break; } cout<<r-1<<endl; while(!ans.empty()){ cout<<ans.top().typ<<" "<<ans.top().x<<" "<<ans.top().k<<endl; ans.pop(); } return 0; } |
English