#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m; cin >> n >> m;
vector<string> g(n);
for(auto& i : g) cin >> i;
struct Count {
int cnt[26] = {};
int distinct=0;
int& operator[](char c) {
return cnt[c-'A'];
}
char get() {
char res='A';
for(char c='A';c<='Z';++c) {
if((*this)[c]) {
res=c;
}
}
return res;
}
void add(char c, int v) {
if(v>0) {
if(! (*this)[c] ){
distinct++;
}
(*this)[c]+=v;
} else {
(*this)[c]+=v;
if(! (*this)[c] ){
distinct--;
}
}
}
};
vector<Count> rs(n), cs(m);
vector<pair<char,int>> q;
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
rs[i].add(g[i][j],1);
cs[j].add(g[i][j],1);
}
}
for(int i=0;i<n;++i) {
if(rs[i].distinct<=1) q.push_back({'R',i});
}
for(int j=0;j<m;++j) {
if(cs[j].distinct<=1) q.push_back({'K',j});
}
vector<tuple<char,int,char>> ans;
for(int rep=0;rep<int(q.size());++rep) {
auto [c,i] = q[rep];
if(c=='R') {
ans.push_back({c,i+1,rs[i].get()});
for(int j=0;j<m;++j) if(g[i][j]!='.') {
bool bad= cs[j].distinct>1;
cs[j].add(g[i][j],-1);
if(cs[j].distinct<=1 and bad) q.push_back({'K',j});
g[i][j]='.';
}
} else {
ans.push_back({c,i+1,cs[i].get()});
for(int j=0;j<n;++j) if(g[j][i]!='.') {
bool bad= rs[j].distinct>1;
rs[j].add(g[j][i],-1);
if(rs[j].distinct<=1 and bad) q.push_back({'R',j});
g[j][i]='.';
}
}
}
reverse(all(ans));
cout << ans.size() << '\n';
for(auto [type,pos,color] : ans) cout << type << ' ' << pos << ' ' << color << '\n';
}
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 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vector<string> g(n); for(auto& i : g) cin >> i; struct Count { int cnt[26] = {}; int distinct=0; int& operator[](char c) { return cnt[c-'A']; } char get() { char res='A'; for(char c='A';c<='Z';++c) { if((*this)[c]) { res=c; } } return res; } void add(char c, int v) { if(v>0) { if(! (*this)[c] ){ distinct++; } (*this)[c]+=v; } else { (*this)[c]+=v; if(! (*this)[c] ){ distinct--; } } } }; vector<Count> rs(n), cs(m); vector<pair<char,int>> q; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { rs[i].add(g[i][j],1); cs[j].add(g[i][j],1); } } for(int i=0;i<n;++i) { if(rs[i].distinct<=1) q.push_back({'R',i}); } for(int j=0;j<m;++j) { if(cs[j].distinct<=1) q.push_back({'K',j}); } vector<tuple<char,int,char>> ans; for(int rep=0;rep<int(q.size());++rep) { auto [c,i] = q[rep]; if(c=='R') { ans.push_back({c,i+1,rs[i].get()}); for(int j=0;j<m;++j) if(g[i][j]!='.') { bool bad= cs[j].distinct>1; cs[j].add(g[i][j],-1); if(cs[j].distinct<=1 and bad) q.push_back({'K',j}); g[i][j]='.'; } } else { ans.push_back({c,i+1,cs[i].get()}); for(int j=0;j<n;++j) if(g[j][i]!='.') { bool bad= rs[j].distinct>1; rs[j].add(g[j][i],-1); if(rs[j].distinct<=1 and bad) q.push_back({'R',j}); g[j][i]='.'; } } } reverse(all(ans)); cout << ans.size() << '\n'; for(auto [type,pos,color] : ans) cout << type << ' ' << pos << ' ' << color << '\n'; } |
English