#include <cstdio> #include <map> using namespace std; int n, m, r; char c; map<char, int> tr[2001], tc[2001]; char rd[4001], rc[4001]; int ri[4001]; void color(char d, int x) { char q; rd[r] = d; ri[r] = x + 1; if (d == 'R') { q = tr[x].begin()->first; rc[r] = q; r++; tr[x].erase(tr[x].begin()); for (int j = 0; j < m; j++) { map<char, int>::iterator it; it = tc[j].find(q); if (it != tc[j].end()) { if (it->second == 1) { tc[j].erase(it); if (tc[j].size() == 1) { color('K', j); } } else { tc[j][q] = tc[j][q] - 1; } } } } else { q = tc[x].begin()->first; rc[r] = q; r++; tc[x].erase(tc[x].begin()); for (int i = 0; i < n; i++) { map<char, int>::iterator it; it = tr[i].find(q); if (it != tr[i].end()) { if (it->second == 1) { tr[i].erase(it); if (tr[i].size() == 1) { color('R', i); } } else { tr[i][q] = tr[i][q] - 1; } } } } } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { do { scanf("%c", &c); } while ((c < 65) || (c > 90)); if (tr[i].find(c) == tr[i].end()) { tr[i][c] = 1; } else { tr[i][c] = tr[i][c] + 1; } if (tc[j].find(c) == tc[j].end()) { tc[j][c] = 1; } else { tc[j][c] = tc[j][c] + 1; } } } r = 0; for (int i = 0; i < n; i++) { if (tr[i].size() == 1) { color('R', i); } } for (int j = 0; j < m; j++) { if (tc[j].size() == 1) { color('K', j); } } printf("%d\n", r); for (int i = r - 1; i >= 0; i--) { printf("%c %d %c\n", rd[i], ri[i], rc[i]); } 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 | #include <cstdio> #include <map> using namespace std; int n, m, r; char c; map<char, int> tr[2001], tc[2001]; char rd[4001], rc[4001]; int ri[4001]; void color(char d, int x) { char q; rd[r] = d; ri[r] = x + 1; if (d == 'R') { q = tr[x].begin()->first; rc[r] = q; r++; tr[x].erase(tr[x].begin()); for (int j = 0; j < m; j++) { map<char, int>::iterator it; it = tc[j].find(q); if (it != tc[j].end()) { if (it->second == 1) { tc[j].erase(it); if (tc[j].size() == 1) { color('K', j); } } else { tc[j][q] = tc[j][q] - 1; } } } } else { q = tc[x].begin()->first; rc[r] = q; r++; tc[x].erase(tc[x].begin()); for (int i = 0; i < n; i++) { map<char, int>::iterator it; it = tr[i].find(q); if (it != tr[i].end()) { if (it->second == 1) { tr[i].erase(it); if (tr[i].size() == 1) { color('R', i); } } else { tr[i][q] = tr[i][q] - 1; } } } } } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { do { scanf("%c", &c); } while ((c < 65) || (c > 90)); if (tr[i].find(c) == tr[i].end()) { tr[i][c] = 1; } else { tr[i][c] = tr[i][c] + 1; } if (tc[j].find(c) == tc[j].end()) { tc[j][c] = 1; } else { tc[j][c] = tc[j][c] + 1; } } } r = 0; for (int i = 0; i < n; i++) { if (tr[i].size() == 1) { color('R', i); } } for (int j = 0; j < m; j++) { if (tc[j].size() == 1) { color('K', j); } } printf("%d\n", r); for (int i = r - 1; i >= 0; i--) { printf("%c %d %c\n", rd[i], ri[i], rc[i]); } return 0; } |