#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair #define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 2e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); void solve() { int n, m; cin >> n >> m; vector<string> a(n); for(auto &i : a) { cin >> i; for(auto &j : i) j -= 'A' - 1; } vector<vector<int>> rows(n, vector<int>(27)); vector<vector<int>> cols(m, vector<int>(27)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { rows[i][a[i][j]]++; cols[j][a[i][j]]++; } } auto good_row = [&](int i) { return *max_element(all(rows[i])) == accumulate(all(rows[i]), 0ll); }; auto good_col = [&](int i) { return *max_element(all(cols[i])) == accumulate(all(cols[i]), 0ll); }; queue<pii> q; for(int i = 0; i < n; i++) { if(good_row(i)) q.push({0, i}); } for(int j = 0; j < m; j++) { if(good_col(j)) q.push({1, j}); } auto update_row = [&](int i, int j) { if(good_row(i) || !a[i][j]) return; rows[i][a[i][j]]--; if(good_row(i)) q.push({0, i}); }; auto update_col = [&](int i, int j) { if(good_col(j) || !a[i][j]) return; cols[j][a[i][j]]--; if(good_col(j)) q.push({1, j}); }; vector<array<int, 3>> ans; while(sz(q)) { auto [type, i] = q.front(); q.pop(); if(type == 0) { if(*max_element(all(rows[i])) == 0) continue; ans.pb({0, i, max_element(all(rows[i])) - rows[i].begin()}); for(int j = 0; j < m; j++) { update_col(i, j); a[i][j] = 0; } } else { if(*max_element(all(cols[i])) == 0) continue; ans.pb({1, i, max_element(all(cols[i])) - cols[i].begin()}); int j = i; for(i = 0; i < n; i++) { update_row(i, j); a[i][j] = 0; } } } reverse(all(ans)); cout << sz(ans) << '\n'; for(auto [x, y, z] : ans) { cout << (x? 'K' : 'R') << " " << y + 1 << " " << char('A' + z - 1) << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } }
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair #define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 2e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); void solve() { int n, m; cin >> n >> m; vector<string> a(n); for(auto &i : a) { cin >> i; for(auto &j : i) j -= 'A' - 1; } vector<vector<int>> rows(n, vector<int>(27)); vector<vector<int>> cols(m, vector<int>(27)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { rows[i][a[i][j]]++; cols[j][a[i][j]]++; } } auto good_row = [&](int i) { return *max_element(all(rows[i])) == accumulate(all(rows[i]), 0ll); }; auto good_col = [&](int i) { return *max_element(all(cols[i])) == accumulate(all(cols[i]), 0ll); }; queue<pii> q; for(int i = 0; i < n; i++) { if(good_row(i)) q.push({0, i}); } for(int j = 0; j < m; j++) { if(good_col(j)) q.push({1, j}); } auto update_row = [&](int i, int j) { if(good_row(i) || !a[i][j]) return; rows[i][a[i][j]]--; if(good_row(i)) q.push({0, i}); }; auto update_col = [&](int i, int j) { if(good_col(j) || !a[i][j]) return; cols[j][a[i][j]]--; if(good_col(j)) q.push({1, j}); }; vector<array<int, 3>> ans; while(sz(q)) { auto [type, i] = q.front(); q.pop(); if(type == 0) { if(*max_element(all(rows[i])) == 0) continue; ans.pb({0, i, max_element(all(rows[i])) - rows[i].begin()}); for(int j = 0; j < m; j++) { update_col(i, j); a[i][j] = 0; } } else { if(*max_element(all(cols[i])) == 0) continue; ans.pb({1, i, max_element(all(cols[i])) - cols[i].begin()}); int j = i; for(i = 0; i < n; i++) { update_row(i, j); a[i][j] = 0; } } } reverse(all(ans)); cout << sz(ans) << '\n'; for(auto [x, y, z] : ans) { cout << (x? 'K' : 'R') << " " << y + 1 << " " << char('A' + z - 1) << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } } |