#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector t(n, vector(m, 0)); constexpr int Z = 26; vector row(n, vector(Z, 0)); vector col(m, vector(Z, 0)); REP (i, n) { REP (j, m) { char c; cin >> c; t[i][j] = c - 'A'; ++row[i][t[i][j]]; ++col[j][t[i][j]]; } } vector<int> rows, cols; REP (i, n) if (*max_element(row[i].begin(), row[i].end()) == m) rows.emplace_back(i); REP (i, m) if (*max_element(col[i].begin(), col[i].end()) == n) cols.emplace_back(i); int rows_left = n; int cols_left = m; vector<int> is_row(n), is_col(m); vector<tuple<char, int, char>> ans; REP (xd, n + m) { if (ssize(rows)) { int r = rows.back(); rows.pop_back(); --rows_left; ans.emplace_back('R', r + 1, char('A' + (max_element(row[r].begin(), row[r].end()) - row[r].begin()))); is_row[r] = 1; REP (i, m) { if (is_col[i]) continue; --col[i][t[r][i]]; if (*max_element(col[i].begin(), col[i].end()) == rows_left) cols.emplace_back(i); } continue; } if (ssize(cols)) { int c = cols.back(); cols.pop_back(); --cols_left; ans.emplace_back('K', c + 1, char('A' + (max_element(col[c].begin(), col[c].end()) - col[c].begin()))); is_col[c] = 1; REP (i, n) { if (is_row[i]) continue; --row[i][t[i][c]]; if (*max_element(row[i].begin(), row[i].end()) == cols_left) rows.emplace_back(i); } continue; } assert(false); } cout << n + m << endl; reverse(ans.begin(), ans.end()); for (auto [a, b, c] : ans) cout << a << ' ' << b << ' ' << c << 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 77 78 79 80 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector t(n, vector(m, 0)); constexpr int Z = 26; vector row(n, vector(Z, 0)); vector col(m, vector(Z, 0)); REP (i, n) { REP (j, m) { char c; cin >> c; t[i][j] = c - 'A'; ++row[i][t[i][j]]; ++col[j][t[i][j]]; } } vector<int> rows, cols; REP (i, n) if (*max_element(row[i].begin(), row[i].end()) == m) rows.emplace_back(i); REP (i, m) if (*max_element(col[i].begin(), col[i].end()) == n) cols.emplace_back(i); int rows_left = n; int cols_left = m; vector<int> is_row(n), is_col(m); vector<tuple<char, int, char>> ans; REP (xd, n + m) { if (ssize(rows)) { int r = rows.back(); rows.pop_back(); --rows_left; ans.emplace_back('R', r + 1, char('A' + (max_element(row[r].begin(), row[r].end()) - row[r].begin()))); is_row[r] = 1; REP (i, m) { if (is_col[i]) continue; --col[i][t[r][i]]; if (*max_element(col[i].begin(), col[i].end()) == rows_left) cols.emplace_back(i); } continue; } if (ssize(cols)) { int c = cols.back(); cols.pop_back(); --cols_left; ans.emplace_back('K', c + 1, char('A' + (max_element(col[c].begin(), col[c].end()) - col[c].begin()))); is_col[c] = 1; REP (i, n) { if (is_row[i]) continue; --row[i][t[i][c]]; if (*max_element(row[i].begin(), row[i].end()) == cols_left) rows.emplace_back(i); } continue; } assert(false); } cout << n + m << endl; reverse(ans.begin(), ans.end()); for (auto [a, b, c] : ans) cout << a << ' ' << b << ' ' << c << endl; } |