#include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define alfa 26 using namespace std; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } void znak(int &c){ c = gc(); while(c < 'A' || c > 'Z') c = gc(); } struct operacja{ char co; int gdzie; char literka; }; void solve(){ int n, m; wczytaj(n), wczytaj(m); vector tab(n+1, vector<int>(m+1)); vector rzad(n+1, vector(alfa, 0)), kol(m+1, vector(alfa, 0)); FOR(i, 1, n) FOR(j, 1, m){ znak(tab[i][j]), tab[i][j] -= 'A'; ++rzad[i][tab[i][j]]; ++kol[j][tab[i][j]]; } set<int> secik_rzad, secik_kol; FOR(i, 1, n) secik_rzad.emplace(i); FOR(i, 1, m) secik_kol.emplace(i); auto rozne = [&](vector<int> &vec){ int ret = 0; REP(i, alfa) if(vec[i]) ++ret; return ret; }; auto kolor = [&](vector<int> &vec){ int ret = 5; REP(i, alfa) if(vec[i]) ret = i; return ret; }; queue<int> q; FOR(i, 1, n) if(rozne(rzad[i]) == 1) q.emplace(i); FOR(i, 1, m) if(rozne(kol[i]) == 1) q.emplace(n+i); vector<operacja> wyn; while(ssize(q)){ int a = q.front(); q.pop(); if(a <= n){ if(!secik_rzad.count(a)) continue; secik_rzad.erase(a); wyn.push_back({'R', a, 'A'+kolor(rzad[a])}); for(int i : secik_kol){ --kol[i][tab[a][i]]; if(rozne(kol[i]) == 1) q.emplace(n+i); } } else{ a -= n; if(!secik_kol.count(a)) continue; secik_kol.erase(a); wyn.push_back({'K', a, 'A'+kolor(kol[a])}); for(int i : secik_rzad){ --rzad[i][tab[i][a]]; if(rozne(rzad[i]) == 1) q.emplace(i); } } } reverse(all(wyn)); printf("%d\n", ssize(wyn)); for(auto [a, b, c] : wyn) printf("%c %d %c\n", a, b, c); } int main(){ 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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define alfa 26 using namespace std; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } void znak(int &c){ c = gc(); while(c < 'A' || c > 'Z') c = gc(); } struct operacja{ char co; int gdzie; char literka; }; void solve(){ int n, m; wczytaj(n), wczytaj(m); vector tab(n+1, vector<int>(m+1)); vector rzad(n+1, vector(alfa, 0)), kol(m+1, vector(alfa, 0)); FOR(i, 1, n) FOR(j, 1, m){ znak(tab[i][j]), tab[i][j] -= 'A'; ++rzad[i][tab[i][j]]; ++kol[j][tab[i][j]]; } set<int> secik_rzad, secik_kol; FOR(i, 1, n) secik_rzad.emplace(i); FOR(i, 1, m) secik_kol.emplace(i); auto rozne = [&](vector<int> &vec){ int ret = 0; REP(i, alfa) if(vec[i]) ++ret; return ret; }; auto kolor = [&](vector<int> &vec){ int ret = 5; REP(i, alfa) if(vec[i]) ret = i; return ret; }; queue<int> q; FOR(i, 1, n) if(rozne(rzad[i]) == 1) q.emplace(i); FOR(i, 1, m) if(rozne(kol[i]) == 1) q.emplace(n+i); vector<operacja> wyn; while(ssize(q)){ int a = q.front(); q.pop(); if(a <= n){ if(!secik_rzad.count(a)) continue; secik_rzad.erase(a); wyn.push_back({'R', a, 'A'+kolor(rzad[a])}); for(int i : secik_kol){ --kol[i][tab[a][i]]; if(rozne(kol[i]) == 1) q.emplace(n+i); } } else{ a -= n; if(!secik_kol.count(a)) continue; secik_kol.erase(a); wyn.push_back({'K', a, 'A'+kolor(kol[a])}); for(int i : secik_rzad){ --rzad[i][tab[i][a]]; if(rozne(rzad[i]) == 1) q.emplace(i); } } } reverse(all(wyn)); printf("%d\n", ssize(wyn)); for(auto [a, b, c] : wyn) printf("%c %d %c\n", a, b, c); } int main(){ solve(); return 0; } |