#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int ALF = 26; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; vector<string> s(n); vector<vi> rowC(n, vi(ALF, 0)); vector<vi> colC(m, vi(ALF, 0)); vi rowF(n, 0); vi colF(m, 0); rep(i, n) { cin >> s[i]; rep(j, m) { rowC[i][s[i][j] - 'A'] += 1; colC[j][s[i][j] - 'A'] += 1; } } vi rowVis(n, 0); vi colVis(m, 0); vector<pii> ready; vector<array<int, 3> > ans; rep(i, n) { rep(j, ALF) if (rowC[i][j]) rowF[i] += 1; if (rowF[i] == 1) ready.push_back({0, i}); } rep(i, m) { rep(j, ALF) if (colC[i][j]) colF[i] += 1; if (colF[i] == 1) ready.push_back({1, i}); } while (sz(ready)) { auto [rc, id] = ready.back(); ready.pop_back(); if (rc == 0) { if (rowVis[id]) continue; rowVis[id] = 1; int cid = 0; rep(i, ALF) if (rowC[id][i]) cid = i; ans.push_back({rc, id, cid}); rep(i, m) if (s[id][i] == ('A' + cid)) { if ((--colC[i][cid]) == 0) { if ((--colF[i]) <= 1) { ready.push_back({1, i}); } } s[id][i] = '.'; rowC[id][cid] -= 1; } } else { if (colVis[id]) continue; colVis[id] = 1; int cid = 0; rep(i, ALF) if (colC[id][i]) cid = i; ans.push_back({rc, id, cid}); rep(i, n) if (s[i][id] == ('A' + cid)) { if ((--rowC[i][cid]) == 0) { if ((--rowF[i]) <= 1) { ready.push_back({0, i}); } } s[i][id] = '.'; colC[id][cid] -= 1; } } } reverse(all(ans)); cout << sz(ans) << '\n'; for (auto [rc, id, ch] : ans) { if (rc == 0) cout << "R "; else cout << "K "; cout << id + 1 << ' '; cout << (char)('A' + ch) << '\n'; } //debug(s); 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 121 122 123 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int ALF = 26; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; vector<string> s(n); vector<vi> rowC(n, vi(ALF, 0)); vector<vi> colC(m, vi(ALF, 0)); vi rowF(n, 0); vi colF(m, 0); rep(i, n) { cin >> s[i]; rep(j, m) { rowC[i][s[i][j] - 'A'] += 1; colC[j][s[i][j] - 'A'] += 1; } } vi rowVis(n, 0); vi colVis(m, 0); vector<pii> ready; vector<array<int, 3> > ans; rep(i, n) { rep(j, ALF) if (rowC[i][j]) rowF[i] += 1; if (rowF[i] == 1) ready.push_back({0, i}); } rep(i, m) { rep(j, ALF) if (colC[i][j]) colF[i] += 1; if (colF[i] == 1) ready.push_back({1, i}); } while (sz(ready)) { auto [rc, id] = ready.back(); ready.pop_back(); if (rc == 0) { if (rowVis[id]) continue; rowVis[id] = 1; int cid = 0; rep(i, ALF) if (rowC[id][i]) cid = i; ans.push_back({rc, id, cid}); rep(i, m) if (s[id][i] == ('A' + cid)) { if ((--colC[i][cid]) == 0) { if ((--colF[i]) <= 1) { ready.push_back({1, i}); } } s[id][i] = '.'; rowC[id][cid] -= 1; } } else { if (colVis[id]) continue; colVis[id] = 1; int cid = 0; rep(i, ALF) if (colC[id][i]) cid = i; ans.push_back({rc, id, cid}); rep(i, n) if (s[i][id] == ('A' + cid)) { if ((--rowC[i][cid]) == 0) { if ((--rowF[i]) <= 1) { ready.push_back({0, i}); } } s[i][id] = '.'; colC[id][cid] -= 1; } } } reverse(all(ans)); cout << sz(ans) << '\n'; for (auto [rc, id, ch] : ans) { if (rc == 0) cout << "R "; else cout << "K "; cout << id + 1 << ' '; cout << (char)('A' + ch) << '\n'; } //debug(s); return 0; } |