// Kacper Orszulak #ifndef DEBUG #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vb = vector<bool>; using vvi = vector<vi>; using vvll = vector<vll>; #define st first #define nd second #define pb push_back #define rep(i, n) for (int i = 0; i < int(n); ++i) #define whole(x) x.begin(), x.end() [[maybe_unused]] constexpr int INF = 1e9+7; template<class T> bool minR(T &a, const T &b) { if (b < a) { a = b; return true; } else return false; } template<class T> bool maxR(T &a, const T &b) { if (b > a) { a = b; return true; } else return false; } struct Step { bool isColumn; int id; int c; }; int n, m; vvi board; vi rcCnter[2][26]; vi rcNonZeroCnt[2]; vi (&rowCnter)[26] = rcCnter[0]; vi (&columnCnter)[26] = rcCnter[1]; vi &rowNonZeroCnt = rcNonZeroCnt[0]; vi &columnNonZeroCnt = rcNonZeroCnt[1]; int& getCharAt(bool isColumn, int i, int j) { if (not isColumn) return board[i][j]; else return board[j][i]; } int findLonely(bool isColumn, int i) { assert(rcNonZeroCnt[isColumn][i] == 1); rep(c, 26) { if (rcCnter[isColumn][c][i] > 0) return c; } // const int LEN = isColumn ? n : m; // rep(j, LEN) { // const int c = getCharAt(isColumn, i, j); // if (c != INF) // return c; // } return INF; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m; board.resize(n, vi(m, INF)); rep(i, 26) { rowCnter[i].resize(n, 0); columnCnter[i].resize(m, 0); } rowNonZeroCnt.resize(n, 0); columnNonZeroCnt.resize(m, 0); rep(i, n) { string s; cin >> s; rep(j, m) { const int c = s[j] - 'A'; board[i][j] = c; if (rowCnter[c][i]++ == 0) ++rowNonZeroCnt[i]; if (columnCnter[c][j]++ == 0) ++columnNonZeroCnt[j]; } } vector<Step> steps; queue<Step> nextStep; rep(i, n) { if (rowNonZeroCnt[i] == 1) { nextStep.push({false, i, findLonely(false, i)}); } } rep(j, m) { if (columnNonZeroCnt[j] == 1) nextStep.push({true, j, findLonely(true, j)}); } while (not nextStep.empty()) { const auto [isColumn, id, c] = nextStep.front(); const int LEN = isColumn ? n : m; nextStep.pop(); if (rcNonZeroCnt[isColumn][id] == 0) continue; steps.push_back({isColumn, id, c}); rcCnter[isColumn][c][id] = 0; --rcNonZeroCnt[isColumn][id]; assert(rcNonZeroCnt[isColumn][id] == 0); rep(i, LEN) { int &d = getCharAt(isColumn, id, i); assert(d == c or d == INF); if (d == INF) continue; if (--rcCnter[not isColumn][d][i] == 0) if (--rcNonZeroCnt[not isColumn][i] == 1) nextStep.push({not isColumn, i, findLonely(not isColumn, i)}); d = INF; } } reverse(whole(steps)); cout << steps.size() << '\n'; for (const auto &[isColumn, id, c] : steps) { assert(c != INF); if (isColumn) cout << "K "; else cout << "R "; cout << id+1 << ' '; cout << char('A' + c) << '\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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | // Kacper Orszulak #ifndef DEBUG #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vb = vector<bool>; using vvi = vector<vi>; using vvll = vector<vll>; #define st first #define nd second #define pb push_back #define rep(i, n) for (int i = 0; i < int(n); ++i) #define whole(x) x.begin(), x.end() [[maybe_unused]] constexpr int INF = 1e9+7; template<class T> bool minR(T &a, const T &b) { if (b < a) { a = b; return true; } else return false; } template<class T> bool maxR(T &a, const T &b) { if (b > a) { a = b; return true; } else return false; } struct Step { bool isColumn; int id; int c; }; int n, m; vvi board; vi rcCnter[2][26]; vi rcNonZeroCnt[2]; vi (&rowCnter)[26] = rcCnter[0]; vi (&columnCnter)[26] = rcCnter[1]; vi &rowNonZeroCnt = rcNonZeroCnt[0]; vi &columnNonZeroCnt = rcNonZeroCnt[1]; int& getCharAt(bool isColumn, int i, int j) { if (not isColumn) return board[i][j]; else return board[j][i]; } int findLonely(bool isColumn, int i) { assert(rcNonZeroCnt[isColumn][i] == 1); rep(c, 26) { if (rcCnter[isColumn][c][i] > 0) return c; } // const int LEN = isColumn ? n : m; // rep(j, LEN) { // const int c = getCharAt(isColumn, i, j); // if (c != INF) // return c; // } return INF; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m; board.resize(n, vi(m, INF)); rep(i, 26) { rowCnter[i].resize(n, 0); columnCnter[i].resize(m, 0); } rowNonZeroCnt.resize(n, 0); columnNonZeroCnt.resize(m, 0); rep(i, n) { string s; cin >> s; rep(j, m) { const int c = s[j] - 'A'; board[i][j] = c; if (rowCnter[c][i]++ == 0) ++rowNonZeroCnt[i]; if (columnCnter[c][j]++ == 0) ++columnNonZeroCnt[j]; } } vector<Step> steps; queue<Step> nextStep; rep(i, n) { if (rowNonZeroCnt[i] == 1) { nextStep.push({false, i, findLonely(false, i)}); } } rep(j, m) { if (columnNonZeroCnt[j] == 1) nextStep.push({true, j, findLonely(true, j)}); } while (not nextStep.empty()) { const auto [isColumn, id, c] = nextStep.front(); const int LEN = isColumn ? n : m; nextStep.pop(); if (rcNonZeroCnt[isColumn][id] == 0) continue; steps.push_back({isColumn, id, c}); rcCnter[isColumn][c][id] = 0; --rcNonZeroCnt[isColumn][id]; assert(rcNonZeroCnt[isColumn][id] == 0); rep(i, LEN) { int &d = getCharAt(isColumn, id, i); assert(d == c or d == INF); if (d == INF) continue; if (--rcCnter[not isColumn][d][i] == 0) if (--rcNonZeroCnt[not isColumn][i] == 1) nextStep.push({not isColumn, i, findLonely(not isColumn, i)}); d = INF; } } reverse(whole(steps)); cout << steps.size() << '\n'; for (const auto &[isColumn, id, c] : steps) { assert(c != INF); if (isColumn) cout << "K "; else cout << "R "; cout << id+1 << ' '; cout << char('A' + c) << '\n'; } } |