#include <iostream> #include <vector> #include <queue> #include <map> using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); int n,m; cin>>n>>m; vector <int> pus(m); map <int,int> iul; vector<map<int,int>> il(n+m,iul); queue <int> kol; vector <string> zw(0); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char a; cin>>a; if(il[i].find(a-'A')==il[i].end()) il[i][a-'A']=1; else il[i][a-'A']++; if(il[n+j].find(a-'A')==il[n+j].end()) il[n+j][a-'A']=1; else il[n+j][a-'A']++; } for(int i=0;i<n+m;i++) { if(il[i].size()==1) kol.push(i); } while(!kol.empty()) { int ni=kol.front(); char mi; kol.pop(); if(il[ni].empty()||il[ni][il[ni].begin()->first]==0) continue; mi=il[ni].begin()->first+'A'; string jacek; if(ni<n) jacek+="R "; else jacek+="K "; if(ni<n) jacek+=to_string(ni+1)+" "+mi+"\n"; else jacek+=to_string(ni+1-n)+" "+mi+"\n"; zw.push_back(jacek); il[ni].erase(il[ni].begin()); if(ni<n) { for(int i=0;i<m;i++) { if(il[n+i].find(mi-'A')!=il[n+i].end()) {il[n+i][mi-'A']--; if(il[i+n].size()>1&&il[i+n][mi-'A']<=0) il[i+n].erase(il[i+n].find(mi-'A'));} if(il[n+i].size()==1&&il[n+i].begin()->second!=0) kol.push(n+i); } } else { for(int i=0;i<n;i++) { if(il[i].find(mi-'A')!=il[i].end()) {il[i][mi-'A']--; if(il[i].size()>1&&il[i][mi-'A']<=0) il[i].erase(il[i].find(mi-'A'));} if(il[i].size()==1&&il[i].begin()->second!=0) kol.push(i); } } } cout<<zw.size()<<"\n"; for(int i=1;i<=zw.size();i++) cout<<zw[zw.size()-i]; }
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 | #include <iostream> #include <vector> #include <queue> #include <map> using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); int n,m; cin>>n>>m; vector <int> pus(m); map <int,int> iul; vector<map<int,int>> il(n+m,iul); queue <int> kol; vector <string> zw(0); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char a; cin>>a; if(il[i].find(a-'A')==il[i].end()) il[i][a-'A']=1; else il[i][a-'A']++; if(il[n+j].find(a-'A')==il[n+j].end()) il[n+j][a-'A']=1; else il[n+j][a-'A']++; } for(int i=0;i<n+m;i++) { if(il[i].size()==1) kol.push(i); } while(!kol.empty()) { int ni=kol.front(); char mi; kol.pop(); if(il[ni].empty()||il[ni][il[ni].begin()->first]==0) continue; mi=il[ni].begin()->first+'A'; string jacek; if(ni<n) jacek+="R "; else jacek+="K "; if(ni<n) jacek+=to_string(ni+1)+" "+mi+"\n"; else jacek+=to_string(ni+1-n)+" "+mi+"\n"; zw.push_back(jacek); il[ni].erase(il[ni].begin()); if(ni<n) { for(int i=0;i<m;i++) { if(il[n+i].find(mi-'A')!=il[n+i].end()) {il[n+i][mi-'A']--; if(il[i+n].size()>1&&il[i+n][mi-'A']<=0) il[i+n].erase(il[i+n].find(mi-'A'));} if(il[n+i].size()==1&&il[n+i].begin()->second!=0) kol.push(n+i); } } else { for(int i=0;i<n;i++) { if(il[i].find(mi-'A')!=il[i].end()) {il[i][mi-'A']--; if(il[i].size()>1&&il[i][mi-'A']<=0) il[i].erase(il[i].find(mi-'A'));} if(il[i].size()==1&&il[i].begin()->second!=0) kol.push(i); } } } cout<<zw.size()<<"\n"; for(int i=1;i<=zw.size();i++) cout<<zw[zw.size()-i]; } |