#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
struct cmd {
int idx;
char c;
};
int main(void)
{
int n, m;
char *data;
std::vector< std::map<char, int > > cnt;
std::vector<cmd> o;
scanf("%d%d", &n, &m);
data = new char[n * m];
for (int i = 0; i < n; i++) {
char buf[2048];
scanf("%s", buf);
memmove(data + i * m, buf, m);
}
cnt.resize(n+m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
char c = data[i * m + j];
cnt[i][c]++;
cnt[n+j][c]++;
}
while (1) {
char c;
int idx = -1;
for (int i = 0; i < n + m; i++)
if (cnt[i].size() == 1 && (idx < 0 || cnt[i].begin()->second > cnt[idx].begin()->second)) {
idx = i;
c = cnt[idx].begin()->first;
}
if (idx < 0)
break;
if (idx < n)
for (int i = 0; i < m; i++) {
if (data[idx * m + i] == c) {
data[idx * m + i] = ' ';
if (--cnt[i+n][c] == 0)
cnt[i+n].erase(c);
}
}
else
for (int i = 0; i < n; i++) {
if (data[i * m + idx-n] == c) {
data[i * m + idx-n] = ' ';
if (--cnt[i][c] == 0)
cnt[i].erase(c);
}
}
cnt[idx].clear();
cmd e = {idx, c};
o.push_back(e);
}
printf("%ld\n", o.size());
for (int i = o.size() - 1; i >= 0; i--) {
cmd &e = o[i];
if (e.idx < n)
printf("R %d %c\n", e.idx + 1, e.c);
else
printf("K %d %c\n", e.idx + 1 - n, e.c);
}
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 | #include <cstdio> #include <cstring> #include <map> #include <vector> struct cmd { int idx; char c; }; int main(void) { int n, m; char *data; std::vector< std::map<char, int > > cnt; std::vector<cmd> o; scanf("%d%d", &n, &m); data = new char[n * m]; for (int i = 0; i < n; i++) { char buf[2048]; scanf("%s", buf); memmove(data + i * m, buf, m); } cnt.resize(n+m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c = data[i * m + j]; cnt[i][c]++; cnt[n+j][c]++; } while (1) { char c; int idx = -1; for (int i = 0; i < n + m; i++) if (cnt[i].size() == 1 && (idx < 0 || cnt[i].begin()->second > cnt[idx].begin()->second)) { idx = i; c = cnt[idx].begin()->first; } if (idx < 0) break; if (idx < n) for (int i = 0; i < m; i++) { if (data[idx * m + i] == c) { data[idx * m + i] = ' '; if (--cnt[i+n][c] == 0) cnt[i+n].erase(c); } } else for (int i = 0; i < n; i++) { if (data[i * m + idx-n] == c) { data[i * m + idx-n] = ' '; if (--cnt[i][c] == 0) cnt[i].erase(c); } } cnt[idx].clear(); cmd e = {idx, c}; o.push_back(e); } printf("%ld\n", o.size()); for (int i = o.size() - 1; i >= 0; i--) { cmd &e = o[i]; if (e.idx < n) printf("R %d %c\n", e.idx + 1, e.c); else printf("K %d %c\n", e.idx + 1 - n, e.c); } return 0; } |
English