#include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,m; cin >> n >> m; vector<string> T(n, string(m, 0)); for(auto &S:T) cin >> S; constexpr int siz = 'Z' - 'A' + 1; vector<array<int,siz>> Rcnt(n); vector<array<int,siz>> Ccnt(m); vector<int> Rdif(n, 0); vector<int> Cdif(m, 0); vector<tuple<int,int,int>> Q; //type, index, letter vector<tuple<int,int,int>> ans; vector<int> Rvis(n, 0); vector<int> Cvis(m, 0); rep(i,0,n) rep(j,0,m) { if(++Rcnt[i][T[i][j] - 'A'] == 1) Rdif[i]++; if(++Ccnt[j][T[i][j] - 'A'] == 1) Cdif[j]++; } auto get_let = [](array<int, siz> const & cnt) { return find_if(all(cnt),[](int x){return x;}) - begin(cnt); }; rep(i,0,n) if(Rdif[i] == 1) Q.emplace_back(0, i, get_let(Rcnt[i])); rep(i,0,m) if(Cdif[i] == 1) Q.emplace_back(1, i, get_let(Ccnt[i])); auto rem_from_row = [&](int ind, int let) { if(--Rcnt[ind][let] == 0) { if(--Rdif[ind] == 1) { Q.emplace_back(0, ind, get_let(Rcnt[ind])); } } }; auto rem_from_col = [&](int ind, int let) { if(--Ccnt[ind][let] == 0) { if(--Cdif[ind] == 1) { Q.emplace_back(1, ind, get_let(Ccnt[ind])); } } }; while(!Q.empty()) { auto [t, ind, let] = Q.back(); Q.pop_back(); ans.emplace_back(t,ind,let); if(t) rep(i,0,n) rem_from_row(i, let); else rep(i,0,m) rem_from_col(i, let); } reverse(all(ans)); cout << ans.size() << '\n'; for(auto [t, ind, let] : ans) cout << (t ? 'K' : 'R') << ' ' << ind + 1 << ' ' << char(let + '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 | #include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,m; cin >> n >> m; vector<string> T(n, string(m, 0)); for(auto &S:T) cin >> S; constexpr int siz = 'Z' - 'A' + 1; vector<array<int,siz>> Rcnt(n); vector<array<int,siz>> Ccnt(m); vector<int> Rdif(n, 0); vector<int> Cdif(m, 0); vector<tuple<int,int,int>> Q; //type, index, letter vector<tuple<int,int,int>> ans; vector<int> Rvis(n, 0); vector<int> Cvis(m, 0); rep(i,0,n) rep(j,0,m) { if(++Rcnt[i][T[i][j] - 'A'] == 1) Rdif[i]++; if(++Ccnt[j][T[i][j] - 'A'] == 1) Cdif[j]++; } auto get_let = [](array<int, siz> const & cnt) { return find_if(all(cnt),[](int x){return x;}) - begin(cnt); }; rep(i,0,n) if(Rdif[i] == 1) Q.emplace_back(0, i, get_let(Rcnt[i])); rep(i,0,m) if(Cdif[i] == 1) Q.emplace_back(1, i, get_let(Ccnt[i])); auto rem_from_row = [&](int ind, int let) { if(--Rcnt[ind][let] == 0) { if(--Rdif[ind] == 1) { Q.emplace_back(0, ind, get_let(Rcnt[ind])); } } }; auto rem_from_col = [&](int ind, int let) { if(--Ccnt[ind][let] == 0) { if(--Cdif[ind] == 1) { Q.emplace_back(1, ind, get_let(Ccnt[ind])); } } }; while(!Q.empty()) { auto [t, ind, let] = Q.back(); Q.pop_back(); ans.emplace_back(t,ind,let); if(t) rep(i,0,n) rem_from_row(i, let); else rep(i,0,m) rem_from_col(i, let); } reverse(all(ans)); cout << ans.size() << '\n'; for(auto [t, ind, let] : ans) cout << (t ? 'K' : 'R') << ' ' << ind + 1 << ' ' << char(let + 'A') << '\n'; return 0; } |