#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n,m; cin >> n >> m; vector<string> list(n); vector<map<char, ll>> mp(n+m); set<ll> st; vector<pair<ll, char>> res; for(auto &el : list) cin >> el; for(ll i = 0; i < n; i++){ for(ll j = 0; j < m; j++){ mp[j][list[i][j]]++; mp[n+i][list[i][j]]++; } } for(ll i = 0; i < n+m; i++){ if(mp[i].size() != 1) continue; st.emplace(i); } while(!st.empty()){ // for(auto el : st) cout << "(" << el.first << "," << el.second << ")"; // cout << endl; ll element = *st.begin(); st.erase(st.begin()); if(mp[element].empty()) continue; res.push_back({element, (*mp[element].begin()).first}); // dbg(element.first, element.second); // cout << element.first << ", " << element.second << endl; // for(auto el : mp[element.second]){ // cout << el.first << " -> " << el.second << endl; // } if(element < n){ for(ll j = n; j < n+m; j++){ if(mp[j].empty()) continue; else if(mp[j][(*mp[element].begin()).first] == 1){ mp[j].erase((*mp[element].begin()).first); } else{ mp[j][(*mp[element].begin()).first]--; } if(mp[j].size() == 1) st.emplace(j); } } else{ for(ll j = 0; j < n; j++){ if(mp[j].empty()) continue; else if(mp[j][(*mp[element].begin()).first] == 1){ mp[j].erase((*mp[element].begin()).first); } else{ mp[j][(*mp[element].begin()).first]--; } if(mp[j].size() == 1) st.emplace(j); } } mp[element].erase((*mp[element].begin()).first); // cout << "dwa\n"; // for(auto el : mp[element.second]){ // cout << el.first << " -> " << el.second << endl; // } // dbg(mp[element.second].size()); } reverse(res.begin(), res.end()); cout << res.size() << endl; for(auto el : res){ if(el.first < n){ cout << "K " << el.first+1 << " " << el.second << endl; } else{ cout << "R " << el.first-n+1 << " " << el.second << endl; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif 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 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n,m; cin >> n >> m; vector<string> list(n); vector<map<char, ll>> mp(n+m); set<ll> st; vector<pair<ll, char>> res; for(auto &el : list) cin >> el; for(ll i = 0; i < n; i++){ for(ll j = 0; j < m; j++){ mp[j][list[i][j]]++; mp[n+i][list[i][j]]++; } } for(ll i = 0; i < n+m; i++){ if(mp[i].size() != 1) continue; st.emplace(i); } while(!st.empty()){ // for(auto el : st) cout << "(" << el.first << "," << el.second << ")"; // cout << endl; ll element = *st.begin(); st.erase(st.begin()); if(mp[element].empty()) continue; res.push_back({element, (*mp[element].begin()).first}); // dbg(element.first, element.second); // cout << element.first << ", " << element.second << endl; // for(auto el : mp[element.second]){ // cout << el.first << " -> " << el.second << endl; // } if(element < n){ for(ll j = n; j < n+m; j++){ if(mp[j].empty()) continue; else if(mp[j][(*mp[element].begin()).first] == 1){ mp[j].erase((*mp[element].begin()).first); } else{ mp[j][(*mp[element].begin()).first]--; } if(mp[j].size() == 1) st.emplace(j); } } else{ for(ll j = 0; j < n; j++){ if(mp[j].empty()) continue; else if(mp[j][(*mp[element].begin()).first] == 1){ mp[j].erase((*mp[element].begin()).first); } else{ mp[j][(*mp[element].begin()).first]--; } if(mp[j].size() == 1) st.emplace(j); } } mp[element].erase((*mp[element].begin()).first); // cout << "dwa\n"; // for(auto el : mp[element.second]){ // cout << el.first << " -> " << el.second << endl; // } // dbg(mp[element.second].size()); } reverse(res.begin(), res.end()); cout << res.size() << endl; for(auto el : res){ if(el.first < n){ cout << "K " << el.first+1 << " " << el.second << endl; } else{ cout << "R " << el.first-n+1 << " " << el.second << endl; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); } |