#include<bits/stdc++.h> using namespace std; struct Counter { int diff = 0; vector<int> cnt = vector<int>(26, 0); void add(char c) { if(c == '-') return; int id = c-'A'; if(cnt[id] == 0) diff++; cnt[id]++; } void del(char c) { if(c == '-') return; int id = c-'A'; cnt[id]--; if(cnt[id] == 0) diff--; } bool empty() { return diff == 0; } bool ready() { return diff <= 1; } char getAny() { for(int i=0;i<26;i++) if(cnt[i] > 0) return i+'A'; return '-'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<string> t(n); for(auto& i : t) cin >> i; vector<tuple<char,int,char>> result; { vector<Counter> row(n), col(m); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { row[i].add(t[i][j]); col[j].add(t[i][j]); } queue<pair<char,int>> q; for(int i=0;i<n;i++) if(row[i].ready()) q.push({'R', i}); for(int j=0;j<m;j++) if(col[j].ready()) q.push({'K', j}); while(!q.empty()) { auto [type, v] = q.front(); q.pop(); if(type == 'R') { if(row[v].empty()) continue; result.push_back({'R', v+1, row[v].getAny()}); for(int j=0;j<m;j++) { row[v].del(t[v][j]); col[j].del(t[v][j]); if(col[j].ready()) q.push({'K', j}); t[v][j] = '-'; } } else { if(col[v].empty()) continue; result.push_back({'K', v+1, col[v].getAny()}); for(int i=0;i<n;i++) { row[i].del(t[i][v]); col[v].del(t[i][v]); if(row[i].ready()) q.push({'R', i}); t[i][v] = '-'; } } } reverse(result.begin(), result.end()); } cout << result.size() << "\n"; for(auto [a,b,c] : result) cout << a << " " << b << " " << c << "\n"; 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 | #include<bits/stdc++.h> using namespace std; struct Counter { int diff = 0; vector<int> cnt = vector<int>(26, 0); void add(char c) { if(c == '-') return; int id = c-'A'; if(cnt[id] == 0) diff++; cnt[id]++; } void del(char c) { if(c == '-') return; int id = c-'A'; cnt[id]--; if(cnt[id] == 0) diff--; } bool empty() { return diff == 0; } bool ready() { return diff <= 1; } char getAny() { for(int i=0;i<26;i++) if(cnt[i] > 0) return i+'A'; return '-'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<string> t(n); for(auto& i : t) cin >> i; vector<tuple<char,int,char>> result; { vector<Counter> row(n), col(m); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { row[i].add(t[i][j]); col[j].add(t[i][j]); } queue<pair<char,int>> q; for(int i=0;i<n;i++) if(row[i].ready()) q.push({'R', i}); for(int j=0;j<m;j++) if(col[j].ready()) q.push({'K', j}); while(!q.empty()) { auto [type, v] = q.front(); q.pop(); if(type == 'R') { if(row[v].empty()) continue; result.push_back({'R', v+1, row[v].getAny()}); for(int j=0;j<m;j++) { row[v].del(t[v][j]); col[j].del(t[v][j]); if(col[j].ready()) q.push({'K', j}); t[v][j] = '-'; } } else { if(col[v].empty()) continue; result.push_back({'K', v+1, col[v].getAny()}); for(int i=0;i<n;i++) { row[i].del(t[i][v]); col[v].del(t[i][v]); if(row[i].ready()) q.push({'R', i}); t[i][v] = '-'; } } } reverse(result.begin(), result.end()); } cout << result.size() << "\n"; for(auto [a,b,c] : result) cout << a << " " << b << " " << c << "\n"; return 0; } |