#include <cstdio> int s[4000], c[4000][26], p[4000]; char r[4000]; int main() { int n, m, q; char a; scanf("%i%i ", &n, &m); for (int i = 0; i<n; i++) { for (int j = n; j<n+m; j++) { scanf("%c", &a); a -= 'A'; c[i][(int)a]++; c[j][(int)a]++; } scanf(" "); } for (int i = 0; i<n+m; i++) for (int k = 0; k<26; k++) s[i] += c[i][k]>0; q = 0; while (1) { int i; for (i = 0; i<n+m; i++) if (s[i] == 1) break; if (i<n+m) { int k; for (k = 0; k<26; k++) if (c[i][k] > 0) break; p[q] = i; r[q++] = k; s[i]--; c[i][k]--; for (int j = n*(i<n); j < n+m*(i<n); j++) if (s[j]>0) s[j] -= !--c[j][k]; } else break; } printf("%i\n", q); for (int i = q-1; i>=0; i--) printf("%c %i %c\n", "RK"[p[i]>=n], 1+p[i]-n*(p[i]>=n), 'A'+r[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 | #include <cstdio> int s[4000], c[4000][26], p[4000]; char r[4000]; int main() { int n, m, q; char a; scanf("%i%i ", &n, &m); for (int i = 0; i<n; i++) { for (int j = n; j<n+m; j++) { scanf("%c", &a); a -= 'A'; c[i][(int)a]++; c[j][(int)a]++; } scanf(" "); } for (int i = 0; i<n+m; i++) for (int k = 0; k<26; k++) s[i] += c[i][k]>0; q = 0; while (1) { int i; for (i = 0; i<n+m; i++) if (s[i] == 1) break; if (i<n+m) { int k; for (k = 0; k<26; k++) if (c[i][k] > 0) break; p[q] = i; r[q++] = k; s[i]--; c[i][k]--; for (int j = n*(i<n); j < n+m*(i<n); j++) if (s[j]>0) s[j] -= !--c[j][k]; } else break; } printf("%i\n", q); for (int i = q-1; i>=0; i--) printf("%c %i %c\n", "RK"[p[i]>=n], 1+p[i]-n*(p[i]>=n), 'A'+r[i]); return 0; } |