#include<iostream> #include<vector> #include<string> #include<deque> #include<list> using namespace std; struct event { char type; int num; char color; }; struct entry { int num; int tab[26]; }; int check(entry &e, int k) { for (int i = 0; i < 26; i++) { if (e.tab[i] == k) { return i; } } return -1; } void remove_row(deque<entry> &cols, char c) { for (entry &e : cols) { e.tab[c - 'A']--; } } void remove_column(deque<entry> &rows, char c) { for (entry &e : rows) { e.tab[c - 'A']--; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); const int nchars = 26; int nrows, ncols; cin >> nrows >> ncols; deque<entry> rows(nrows); deque<entry> cols(ncols); for (int i = 0; i < nrows; i++) { string s; cin >> s; for (int j = 0; j < ncols; j++) { char c = s[j]; rows[i].num = i + 1; rows[i].tab[c - 'A']++; cols[j].num = j + 1; cols[j].tab[c - 'A']++; } } vector<event> events; while (nrows > 0 && ncols > 0) { // rows auto it = rows.end(); for (it = rows.begin(); it != rows.end(); it++) { entry e = *it; int pos = check(e, ncols); if (pos != -1) { char c = 'A' + pos; events.push_back({'R', e.num, c}); remove_row(cols, c); nrows--; break; } } if (it != rows.end()) { rows.erase(it); continue; } // columns it = cols.end(); for (it = cols.begin(); it != cols.end(); it++) { entry e = *it; int pos = check(e, nrows); if (pos != -1) { char c = 'A' + pos; events.push_back({'K', e.num, c}); remove_column(rows, c); ncols--; break; } } if (it != cols.end()) { cols.erase(it); continue; } } cout << events.size() << "\n"; for (int i = events.size() - 1; i >= 0; i--) { event &e = events[i]; cout << e.type << ' ' << e.num << ' ' << e.color << "\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 101 102 103 104 105 106 107 108 109 110 111 112 | #include<iostream> #include<vector> #include<string> #include<deque> #include<list> using namespace std; struct event { char type; int num; char color; }; struct entry { int num; int tab[26]; }; int check(entry &e, int k) { for (int i = 0; i < 26; i++) { if (e.tab[i] == k) { return i; } } return -1; } void remove_row(deque<entry> &cols, char c) { for (entry &e : cols) { e.tab[c - 'A']--; } } void remove_column(deque<entry> &rows, char c) { for (entry &e : rows) { e.tab[c - 'A']--; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); const int nchars = 26; int nrows, ncols; cin >> nrows >> ncols; deque<entry> rows(nrows); deque<entry> cols(ncols); for (int i = 0; i < nrows; i++) { string s; cin >> s; for (int j = 0; j < ncols; j++) { char c = s[j]; rows[i].num = i + 1; rows[i].tab[c - 'A']++; cols[j].num = j + 1; cols[j].tab[c - 'A']++; } } vector<event> events; while (nrows > 0 && ncols > 0) { // rows auto it = rows.end(); for (it = rows.begin(); it != rows.end(); it++) { entry e = *it; int pos = check(e, ncols); if (pos != -1) { char c = 'A' + pos; events.push_back({'R', e.num, c}); remove_row(cols, c); nrows--; break; } } if (it != rows.end()) { rows.erase(it); continue; } // columns it = cols.end(); for (it = cols.begin(); it != cols.end(); it++) { entry e = *it; int pos = check(e, nrows); if (pos != -1) { char c = 'A' + pos; events.push_back({'K', e.num, c}); remove_column(rows, c); ncols--; break; } } if (it != cols.end()) { cols.erase(it); continue; } } cout << events.size() << "\n"; for (int i = events.size() - 1; i >= 0; i--) { event &e = events[i]; cout << e.type << ' ' << e.num << ' ' << e.color << "\n"; } return 0; } |