#include <algorithm> #include <cstdio> #include <map> #include <queue> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) char a[2000][2001]; bool rd[2000], kd[2000]; int main() { INT(n); INT(m); REP(i,n) scanf("%s", a[i]); vector<map<char, int>> r(n), k(m); REP(i,n) REP(j,m) { ++r[i][a[i][j]]; ++k[j][a[i][j]]; } queue<pair<bool, int> > q; REP(i,n) if (SIZE(r[i]) == 1) { rd[i] = 1; q.push(MP(false, i)); } REP(j,m) if (SIZE(k[j]) == 1) { kd[j] = 1; q.push(MP(true, j)); } vector<pair<pair<bool, int>, char> > res; while (!q.empty()) { pair<bool, int> p = q.front(); q.pop(); if (p.FT) { if (k[p.SD].empty()) continue; char c = k[p.SD].begin()->FT; REP(i,n) if (!r[i].empty() && !--r[i][c]) { r[i].erase(c); if (SIZE(r[i]) == 1 && !rd[i]) { rd[i] = 1; q.push(MP(false, i)); } } k[p.SD].clear(); res.PB(MP(p, c)); } else { if (r[p.SD].empty()) continue; char c = r[p.SD].begin()->FT; REP(j,m) if (!k[j].empty() && !--k[j][c]) { k[j].erase(c); if (SIZE(k[j]) == 1 && !kd[j]) { kd[j] = 1; q.push(MP(true, j)); } } r[p.SD].clear(); res.PB(MP(p, c)); } } reverse(ALL(res)); printf("%d\n", SIZE(res)); FORE(it,res) printf("%c %d %c\n", it->FT.FT ? 'K' : 'R', it->FT.SD + 1, it->SD); }
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 | #include <algorithm> #include <cstdio> #include <map> #include <queue> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) char a[2000][2001]; bool rd[2000], kd[2000]; int main() { INT(n); INT(m); REP(i,n) scanf("%s", a[i]); vector<map<char, int>> r(n), k(m); REP(i,n) REP(j,m) { ++r[i][a[i][j]]; ++k[j][a[i][j]]; } queue<pair<bool, int> > q; REP(i,n) if (SIZE(r[i]) == 1) { rd[i] = 1; q.push(MP(false, i)); } REP(j,m) if (SIZE(k[j]) == 1) { kd[j] = 1; q.push(MP(true, j)); } vector<pair<pair<bool, int>, char> > res; while (!q.empty()) { pair<bool, int> p = q.front(); q.pop(); if (p.FT) { if (k[p.SD].empty()) continue; char c = k[p.SD].begin()->FT; REP(i,n) if (!r[i].empty() && !--r[i][c]) { r[i].erase(c); if (SIZE(r[i]) == 1 && !rd[i]) { rd[i] = 1; q.push(MP(false, i)); } } k[p.SD].clear(); res.PB(MP(p, c)); } else { if (r[p.SD].empty()) continue; char c = r[p.SD].begin()->FT; REP(j,m) if (!k[j].empty() && !--k[j][c]) { k[j].erase(c); if (SIZE(k[j]) == 1 && !kd[j]) { kd[j] = 1; q.push(MP(true, j)); } } r[p.SD].clear(); res.PB(MP(p, c)); } } reverse(ALL(res)); printf("%d\n", SIZE(res)); FORE(it,res) printf("%c %d %c\n", it->FT.FT ? 'K' : 'R', it->FT.SD + 1, it->SD); } |