#include <iostream> #include <vector> #include <set> #include <algorithm> #include <map> long long n,m; int pla[2000][2000]; std::vector<std::map<char,int>> kol,wie; std::vector<std::string> result; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin>>n>>m; char x; wie.resize(n); kol.resize(m); for(int i=0;i<n;i++) for(int j=0;j<m;j++){ std::cin>>x; //std::cout<<i<<j<<x<<std::endl; if(wie[i].count(x)==0) wie[i][x]=1; else wie[i][x]++; if(kol[j].count(x)==0) kol[j][x]=1; else kol[j][x]++; pla[i][j]=x; } int wi=n; int ko=m; while(wi!=0 && ko!=0){ //std::cerr<<wi<<" "<<ko<<std::endl; for(int i=0;i<m;i++) if(!kol[i].empty()&& kol[i].begin()->second==wi) { char kolor= kol[i].begin()->first; result.emplace_back("K " + std::to_string(i + 1) + " " + kolor); //std::cout<<result.back()<<std::endl; for(int j=0;j<n;j++) { if(wie[j][pla[j][i]]==1) wie[j].erase(pla[j][i]); else wie[j][pla[j][i]]--; } kol[i][kolor]=1000000; ko--; } for(int i=0;i<n;i++) if(!wie[i].empty()&& wie[i].begin()->second==ko) { //std::cout<<wie[i].size()<<std::endl; char kolor= wie[i].begin()->first; result.emplace_back("R " + std::to_string(i + 1) + " " + kolor); //std::cout<<result.back()<<std::endl; for(int j=0;j<m;j++) { if(kol[j][pla[i][j]]==1) kol[j].erase(pla[i][j]); else kol[j][pla[i][j]]--; } wie[i][kolor]=1000000; wi--; } } std::cout<<result.size()<<std::endl; for(int i=result.size()-1;i>=0;i--){ std::cout<<result[i]<<std::endl; } }
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 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <map> long long n,m; int pla[2000][2000]; std::vector<std::map<char,int>> kol,wie; std::vector<std::string> result; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin>>n>>m; char x; wie.resize(n); kol.resize(m); for(int i=0;i<n;i++) for(int j=0;j<m;j++){ std::cin>>x; //std::cout<<i<<j<<x<<std::endl; if(wie[i].count(x)==0) wie[i][x]=1; else wie[i][x]++; if(kol[j].count(x)==0) kol[j][x]=1; else kol[j][x]++; pla[i][j]=x; } int wi=n; int ko=m; while(wi!=0 && ko!=0){ //std::cerr<<wi<<" "<<ko<<std::endl; for(int i=0;i<m;i++) if(!kol[i].empty()&& kol[i].begin()->second==wi) { char kolor= kol[i].begin()->first; result.emplace_back("K " + std::to_string(i + 1) + " " + kolor); //std::cout<<result.back()<<std::endl; for(int j=0;j<n;j++) { if(wie[j][pla[j][i]]==1) wie[j].erase(pla[j][i]); else wie[j][pla[j][i]]--; } kol[i][kolor]=1000000; ko--; } for(int i=0;i<n;i++) if(!wie[i].empty()&& wie[i].begin()->second==ko) { //std::cout<<wie[i].size()<<std::endl; char kolor= wie[i].begin()->first; result.emplace_back("R " + std::to_string(i + 1) + " " + kolor); //std::cout<<result.back()<<std::endl; for(int j=0;j<m;j++) { if(kol[j][pla[i][j]]==1) kol[j].erase(pla[i][j]); else kol[j][pla[i][j]]--; } wie[i][kolor]=1000000; wi--; } } std::cout<<result.size()<<std::endl; for(int i=result.size()-1;i>=0;i--){ std::cout<<result[i]<<std::endl; } } |