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
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

constexpr auto CNUM = 'Z' - 'A' + 1;

struct Operation {
    bool isRow;
    int i;
    char col;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;

    vector<vector<int>> r(n, vector<int>(CNUM));  // r[row][color] = count
    vector<vector<int>> c(m, vector<int>(CNUM));  // c[col][color] = count

    vector<bool> delR(n);
    vector<bool> delC(m);
    vector<int> rD(CNUM);  // rD[color] = deleted rows cnt
    vector<int> cD(CNUM);  // cD[color] = deleted cols cnt

    int rDTotal = 0;
    int cDTotal = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char col;
            cin >> col;
            col -= 'A';
            r[i][col]++;
            c[j][col]++;
        }
    }

    vector<Operation> res;

    while (rDTotal < n && cDTotal < m) {
        bool found = false;
        // try to find row to delete
        for (int i = 0; i < r.size(); i++) {
            for (char k = 0; k < CNUM; k++) {
                if (!delR[i] && r[i][k] - cD[k] == m - cDTotal) {
                    rDTotal++;
                    rD[k]++;
                    delR[i] = true;
                    res.push_back(Operation{true, i + 1, (char)(k + 'A')});
                    found = true;
                }
            }
        }
        if (found) {
            continue;
        }
        // try to find column to delete
        for (int i = 0; i < c.size(); i++) {
            for (char k = 0; k < CNUM; k++) {
                if (!delC[i] && c[i][k] - rD[k] == n - rDTotal) {
                    cDTotal++;
                    cD[k]++;
                    delC[i] = true;
                    res.push_back(Operation{false, i + 1, (char)(k + 'A')});
                }
            }
        }
    }

    cout << res.size() << '\n';

    for (int i = res.size() - 1; i >= 0; i--) {
        if (res[i].isRow) {
            cout << "R " << res[i].i << ' ' << res[i].col << '\n';
        } else {
            cout << "K " << res[i].i << ' ' << res[i].col << '\n';
        }
    }

    return 0;
}