#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #define pb emplace_back #define ins insert #define mp make_pair #define ssize(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pii pair <int, int> #define pll pair <ll, ll> #define pld pair <ld, ld> #define st first #define nd second using namespace std; using ll = int_fast64_t; // using lll = __int128_t; using ld = long double; const int oo = 1e9 + 7; const ll ool = 1e18; const int mod = 1e9 + 7; void solve(){ int n, m; cin >> n >> m; vector tab(n, vector<int>(m)); vector cnt(n + m, vector<int>(26, 0)); for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ char c; cin >> c; tab[i][j] = c - 'A'; ++ cnt[i][c - 'A']; ++ cnt[j + n][c - 'A']; } } queue <pair<int, char>> q; auto check = [&](int x){ pair <int, char> is = mp(0, ' '); for(int i = 0; i < 26; i ++){ if(cnt[x][i] > 0){ ++ is.st; is.nd = i; } } return is; }; vector <bool> vis(n + m); for(int i = 0; i < n + m; i ++){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } vector <pair<int, char>> ans; while(ssize(q)){ auto [idx, c] = q.front(); q.pop(); ans.pb(idx, c); if(idx < n){ for(int i = n; i < n + m; i ++){ -- cnt[i][tab[idx][i - n]]; if(!vis[i]){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } } } else{ for(int i = 0; i < n; i ++){ -- cnt[i][tab[i][idx - n]]; if(!vis[i]){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } } } } reverse(all(ans)); cout << ssize(ans) << '\n'; for(auto [idx, c]: ans){ if(idx < n) cout << "R " << idx + 1 << ' ' << char(c + 'A') << '\n'; else cout << "K " << idx - n + 1 << ' ' << char(c + 'A') << '\n'; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; t = 1; // cin >> t; while(t --) solve(); 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 | #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #define pb emplace_back #define ins insert #define mp make_pair #define ssize(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pii pair <int, int> #define pll pair <ll, ll> #define pld pair <ld, ld> #define st first #define nd second using namespace std; using ll = int_fast64_t; // using lll = __int128_t; using ld = long double; const int oo = 1e9 + 7; const ll ool = 1e18; const int mod = 1e9 + 7; void solve(){ int n, m; cin >> n >> m; vector tab(n, vector<int>(m)); vector cnt(n + m, vector<int>(26, 0)); for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ char c; cin >> c; tab[i][j] = c - 'A'; ++ cnt[i][c - 'A']; ++ cnt[j + n][c - 'A']; } } queue <pair<int, char>> q; auto check = [&](int x){ pair <int, char> is = mp(0, ' '); for(int i = 0; i < 26; i ++){ if(cnt[x][i] > 0){ ++ is.st; is.nd = i; } } return is; }; vector <bool> vis(n + m); for(int i = 0; i < n + m; i ++){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } vector <pair<int, char>> ans; while(ssize(q)){ auto [idx, c] = q.front(); q.pop(); ans.pb(idx, c); if(idx < n){ for(int i = n; i < n + m; i ++){ -- cnt[i][tab[idx][i - n]]; if(!vis[i]){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } } } else{ for(int i = 0; i < n; i ++){ -- cnt[i][tab[i][idx - n]]; if(!vis[i]){ auto rn = check(i); if(rn.st == 1){ q.emplace(i, rn.nd); vis[i] = 1; } } } } } reverse(all(ans)); cout << ssize(ans) << '\n'; for(auto [idx, c]: ans){ if(idx < n) cout << "R " << idx + 1 << ' ' << char(c + 'A') << '\n'; else cout << "K " << idx - n + 1 << ' ' << char(c + 'A') << '\n'; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; t = 1; // cin >> t; while(t --) solve(); return 0; } |