//ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define F first #define S second #define ll long long #define N 2010 using namespace std; ll n,m,numr[N][26],numc[N][26],hvr[N],hvc[N]; string s[N]; bool visr[N],visc[N]; vector<pair<pair<ll,ll>,ll> > ans; int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); ll i,j,t; cin>>n>>m; for(i=0;i<n;i++) { cin>>s[i]; for(j=0;j<m;j++) { if(!numr[i][s[i][j]-'A']) { hvr[i]++; } numr[i][s[i][j]-'A']++; if(!numc[j][s[i][j]-'A']) { hvc[j]++; } numc[j][s[i][j]-'A']++; } } for(t=0;t<n+m;t++) { ll ok=-1; for(i=0;i<n;i++) { if(!visr[i]) { if(hvr[i]<=1) { ok=i; break; } } } if(ok!=-1) { for(i=0;i<26;i++) { if(numr[ok][i]>0) { break; } } i%=26; ans.push_back(make_pair(make_pair(0,ok),i)); visr[ok]=true; for(i=0;i<m;i++) { numc[i][s[ok][i]-'A']--; if(numc[i][s[ok][i]-'A']==0) { hvc[i]--; } } continue; } for(i=0;i<m;i++) { if(!visc[i]) { if(hvc[i]<=1) { ok=i; break; } } } if(ok!=-1) { for(i=0;i<26;i++) { if(numc[ok][i]>0) { break; } } i%=26; ans.push_back(make_pair(make_pair(1,ok),i)); visc[ok]=true; for(i=0;i<n;i++) { numr[i][s[i][ok]-'A']--; if(numr[i][s[i][ok]-'A']==0) { hvr[i]--; } } continue; } } cout<<ans.size()<<'\n'; for(i=ans.size()-1;i>=0;i--) { cout<<(ans[i].F.F?'K':'R')<<" "<<ans[i].F.S+1<<" "<<(char)(ans[i].S+'A')<<'\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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | //ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define F first #define S second #define ll long long #define N 2010 using namespace std; ll n,m,numr[N][26],numc[N][26],hvr[N],hvc[N]; string s[N]; bool visr[N],visc[N]; vector<pair<pair<ll,ll>,ll> > ans; int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); ll i,j,t; cin>>n>>m; for(i=0;i<n;i++) { cin>>s[i]; for(j=0;j<m;j++) { if(!numr[i][s[i][j]-'A']) { hvr[i]++; } numr[i][s[i][j]-'A']++; if(!numc[j][s[i][j]-'A']) { hvc[j]++; } numc[j][s[i][j]-'A']++; } } for(t=0;t<n+m;t++) { ll ok=-1; for(i=0;i<n;i++) { if(!visr[i]) { if(hvr[i]<=1) { ok=i; break; } } } if(ok!=-1) { for(i=0;i<26;i++) { if(numr[ok][i]>0) { break; } } i%=26; ans.push_back(make_pair(make_pair(0,ok),i)); visr[ok]=true; for(i=0;i<m;i++) { numc[i][s[ok][i]-'A']--; if(numc[i][s[ok][i]-'A']==0) { hvc[i]--; } } continue; } for(i=0;i<m;i++) { if(!visc[i]) { if(hvc[i]<=1) { ok=i; break; } } } if(ok!=-1) { for(i=0;i<26;i++) { if(numc[ok][i]>0) { break; } } i%=26; ans.push_back(make_pair(make_pair(1,ok),i)); visc[ok]=true; for(i=0;i<n;i++) { numr[i][s[i][ok]-'A']--; if(numr[i][s[i][ok]-'A']==0) { hvr[i]--; } } continue; } } cout<<ans.size()<<'\n'; for(i=ans.size()-1;i>=0;i--) { cout<<(ans[i].F.F?'K':'R')<<" "<<ans[i].F.S+1<<" "<<(char)(ans[i].S+'A')<<'\n'; } return 0; } |