#include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin auto operator<<(auto&o,auto p)->decltype(p.first,o){return o<<'{'<<p.first<<", "<<p.second<<'}';} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';} #ifdef DEBUG #define PF(x...)fprintf(stderr,x) #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define PF(...)(void)0 #define LOG(...)(void)0 #endif #define FOR(a,b,c)for(int a=(b);a<=(c);++a) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=int64_t; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector board(n, vector<int8_t>(m)); REP(i, n) REP(j, m) { char c; cin >> c; board[i][j] = c - 'A'; } int N = n + m; vector<array<int, 26>> maps(N); REP(i, n) REP(j, m) { ++maps[i][board[i][j]]; ++maps[n + j][board[i][j]]; } vector<int> cnt(N); auto notzero = [](int x){return x;}; REP(i, N) cnt[i] = (int)count_if(ALL(maps[i]), notzero); queue<int> Q; auto rm = [&](int v, int8_t x) { if (--maps[v][x] == 0) if (--cnt[v] == 1) Q.push(v); }; vector<pair<int, int8_t>> ops; REP(i, N) if (cnt[i] == 1) Q.push(i); while (!Q.empty()) { int v = Q.front(); Q.pop(); if (!cnt[v]) continue; if (v < n) { REP(i, m) if (board[v][i] != -1) rm(n + i, board[v][i]); } else { REP(i, n) if (board[i][v - n] != -1) rm(i, board[i][v - n]); } ops.emplace_back(v, (int8_t)(find_if(ALL(maps[v]), notzero) - maps[v].begin())); } reverse(ALL(ops)); cout << ssize(ops) << '\n'; for (auto [v, val] : ops) { if (v < n) cout << "R " << v + 1 << ' '; else cout << "K " << v - n + 1 << ' '; cout << (char)('A' + val) << '\n'; } }
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 | #include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin auto operator<<(auto&o,auto p)->decltype(p.first,o){return o<<'{'<<p.first<<", "<<p.second<<'}';} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';} #ifdef DEBUG #define PF(x...)fprintf(stderr,x) #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define PF(...)(void)0 #define LOG(...)(void)0 #endif #define FOR(a,b,c)for(int a=(b);a<=(c);++a) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=int64_t; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector board(n, vector<int8_t>(m)); REP(i, n) REP(j, m) { char c; cin >> c; board[i][j] = c - 'A'; } int N = n + m; vector<array<int, 26>> maps(N); REP(i, n) REP(j, m) { ++maps[i][board[i][j]]; ++maps[n + j][board[i][j]]; } vector<int> cnt(N); auto notzero = [](int x){return x;}; REP(i, N) cnt[i] = (int)count_if(ALL(maps[i]), notzero); queue<int> Q; auto rm = [&](int v, int8_t x) { if (--maps[v][x] == 0) if (--cnt[v] == 1) Q.push(v); }; vector<pair<int, int8_t>> ops; REP(i, N) if (cnt[i] == 1) Q.push(i); while (!Q.empty()) { int v = Q.front(); Q.pop(); if (!cnt[v]) continue; if (v < n) { REP(i, m) if (board[v][i] != -1) rm(n + i, board[v][i]); } else { REP(i, n) if (board[i][v - n] != -1) rm(i, board[i][v - n]); } ops.emplace_back(v, (int8_t)(find_if(ALL(maps[v]), notzero) - maps[v].begin())); } reverse(ALL(ops)); cout << ssize(ops) << '\n'; for (auto [v, val] : ops) { if (v < n) cout << "R " << v + 1 << ' '; else cout << "K " << v - n + 1 << ' '; cout << (char)('A' + val) << '\n'; } } |