#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e2 + 10; const int P = 101; const ll MLL = 1e18; const ll LOG = 31; vector<map<char, int>> mapN, mapM; vector<set<char>> setN, setM; vector<vector<char>> mat; vector<pair<int , int>> er; int n , m; struct Item{ int vl , ind; char a; }; vector<Item> ans; void updM(int in) { if(setM[in].size() == 0) { return; } char a = *(setM[in].begin()); setM[in].clear(); for(int i = 0; i < m; i++) { if(mat[in][i] == a) { mapN[i][a]--; if(mapN[i][a] == 0) { setN[i].erase(setN[i].find(a)); mapN[i].erase(mapN[i].find(a)); if(setN[i].size() == 1) { er.push_back({0, i}); } } } mat[in][i] = '0'; } ans.push_back({1, in, a}); } void updN(int in) { if(setN[in].size() == 0) { return; } char a = *(setN[in].begin()); setN[in].clear(); for(int i = 0; i < n; i++) { if(mat[i][in] == a) { mapM[i][a]--; if(mapM[i][a] == 0) { setM[i].erase(setM[i].find(a)); mapM[i].erase(mapM[i].find(a)); if(setM[i].size() == 1) { er.push_back({1, i}); } } } mat[i][in] = '0'; } ans.push_back({0, in, a}); } void solve() { cin >> n >> m; mapN.resize(m), mapM.resize(n); setN.resize(m), setM.resize(n); mat.resize(n, vector<char>(m)); for(int i = 0; i < n ;i++) { for(int j = 0; j < m; j++) { char a; cin >> a; mat[i][j] = a; if(mapN[j].find(a) == mapN[j].end()) { mapN[j][a] = 0; setN[j].insert(a); } if(mapM[i].find(a) == mapM[i].end()) { mapM[i][a] = 0; setM[i].insert(a); } mapN[j][a]++; mapM[i][a]++; } } for(int i = 0; i < m;i++) { if(setN[i].size() == 1) er.push_back({0 , i}); } for(int i = 0; i < n;i++) { if(setM[i].size() == 1) er.push_back({1 , i}); } while(er.size() > 0) { pair<int , int> last = er[er.size() - 1]; er.pop_back(); if(last.first == 0) updN(last.second); else updM(last.second); } cout << ans.size() << endl; for(int i = ans.size() - 1; i >= 0; i--) { if(ans[i].vl == 0) cout << "K "; else cout << "R "; cout << ans[i].ind + 1 << " " << ans[i].a << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e2 + 10; const int P = 101; const ll MLL = 1e18; const ll LOG = 31; vector<map<char, int>> mapN, mapM; vector<set<char>> setN, setM; vector<vector<char>> mat; vector<pair<int , int>> er; int n , m; struct Item{ int vl , ind; char a; }; vector<Item> ans; void updM(int in) { if(setM[in].size() == 0) { return; } char a = *(setM[in].begin()); setM[in].clear(); for(int i = 0; i < m; i++) { if(mat[in][i] == a) { mapN[i][a]--; if(mapN[i][a] == 0) { setN[i].erase(setN[i].find(a)); mapN[i].erase(mapN[i].find(a)); if(setN[i].size() == 1) { er.push_back({0, i}); } } } mat[in][i] = '0'; } ans.push_back({1, in, a}); } void updN(int in) { if(setN[in].size() == 0) { return; } char a = *(setN[in].begin()); setN[in].clear(); for(int i = 0; i < n; i++) { if(mat[i][in] == a) { mapM[i][a]--; if(mapM[i][a] == 0) { setM[i].erase(setM[i].find(a)); mapM[i].erase(mapM[i].find(a)); if(setM[i].size() == 1) { er.push_back({1, i}); } } } mat[i][in] = '0'; } ans.push_back({0, in, a}); } void solve() { cin >> n >> m; mapN.resize(m), mapM.resize(n); setN.resize(m), setM.resize(n); mat.resize(n, vector<char>(m)); for(int i = 0; i < n ;i++) { for(int j = 0; j < m; j++) { char a; cin >> a; mat[i][j] = a; if(mapN[j].find(a) == mapN[j].end()) { mapN[j][a] = 0; setN[j].insert(a); } if(mapM[i].find(a) == mapM[i].end()) { mapM[i][a] = 0; setM[i].insert(a); } mapN[j][a]++; mapM[i][a]++; } } for(int i = 0; i < m;i++) { if(setN[i].size() == 1) er.push_back({0 , i}); } for(int i = 0; i < n;i++) { if(setM[i].size() == 1) er.push_back({1 , i}); } while(er.size() > 0) { pair<int , int> last = er[er.size() - 1]; er.pop_back(); if(last.first == 0) updN(last.second); else updM(last.second); } cout << ans.size() << endl; for(int i = ans.size() - 1; i >= 0; i--) { if(ans[i].vl == 0) cout << "K "; else cout << "R "; cout << ans[i].ind + 1 << " " << ans[i].a << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; } |