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
#include <bits/stdc++.h>

using namespace std;

struct Event {
    bool type;
    int i, letter;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int Y, X;
    cin >> Y >> X;

    vector<vector<int>> rows(Y, vector<int>(26, 0));
    vector<vector<int>> cols(X, vector<int>(26, 0));
    for (int y = 0; y < Y; y++) {
        string row;
        cin >> row;
        for (int x = 0; x < X; x++) {
            int letter = row[x] - 'A';
            rows[y][letter]++;
            cols[x][letter]++;
        }
    }

    vector<Event> result;
    vector<bool> usedRow(Y, false);
    vector<bool> usedCol(X, false);
    int cy = Y, cx = X;
    while (true) {
        bool flag = false;
        for (int y = 0; y < Y; y++)
            for (int letter = 0; letter < 26; letter++)
                if (rows[y][letter] > 0 and not usedRow[y])
                    flag = true;
        for (int x = 0; x < X; x++)
            for (int letter = 0; letter < 26; letter++)
                if (cols[x][letter] > 0 and not usedCol[x])
                    flag = true;
        if (not flag)
            break;

        flag = false;
        for (int y = 0; y < Y; y++) {
            if (usedRow[y])
                continue;

            for (int letter = 0; letter < 26; letter++)
                if (rows[y][letter] == cx) {
                    for (int x = 0; x < X; x++)
                        cols[x][letter]--;
                    flag = true;
                    usedRow[y] = true;
                    cy--;
                    result.push_back({false, y, letter});
                    break;
                }
        }

        if (flag)
            continue;

        for (int x = 0; x < X; x++) {
            if (usedCol[x])
                continue;

            for (int letter = 0; letter < 26; letter++)
                if (cols[x][letter] == cy) {
                    for (int y = 0; y < Y; y++)
                        rows[y][letter]--;
                    usedCol[x] = true;
                    cx--;
                    result.push_back({true, x, letter});
                    break;
                }
        }
    }

    reverse(result.begin(), result.end());

    cout << result.size() << "\n";
    for (auto &pair : result)
        cout << (pair.type ? "K " : "R ") << pair.i + 1 << " " << char(pair.letter + 'A') << "\n";

    return 0;
}