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

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<std::pair<int, int>>> rows(26, std::vector<std::pair<int, int>>(n));
    std::vector<std::vector<std::pair<int, int>>> cols(26, std::vector<std::pair<int, int>>(m));
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < n; j++) rows[i][j] = {0, j+1};
        for (int j = 0; j < m; j++) cols[i][j] = {0, j+1};
    }

    for (int i = 0; i < n; i++) {
        std::string row;
        std::cin >> row;
        for (int j = 0; j < m; j++) {
            rows[row[j] - 'A'][i].first++;
            cols[row[j] - 'A'][j].first++;
        }
    }

    for (int i = 0; i < 26; i++) {
        std::sort(rows[i].begin(), rows[i].end(), [](auto a, auto b){return a.first > b.first;});
        std::sort(cols[i].begin(), cols[i].end(), [](auto a, auto b){return a.first > b.first;});
    }

    std::vector<std::tuple<char, int, char>> solution;
    std::vector<int> rows_d(26, 0), cols_d(26, 0);
    std::vector<int> rows_i(26, 0), cols_i(26, 0);
    int rows_left = n, cols_left = m;
    for (int i = 0; i < n+m; i++) {
        for (int j = 0; j < 26; j++) {
            if (rows_i[j] < n && rows[j][rows_i[j]].first + rows_d[j] == cols_left) {
                solution.push_back({'R', rows[j][rows_i[j]].second, (char)(j+'A')});
                cols_d[j]--;
                rows_left--;
                rows_i[j]++;
                break;
            }
            if (cols_i[j] < m && cols[j][cols_i[j]].first + cols_d[j] == rows_left) {
                solution.push_back({'K', cols[j][cols_i[j]].second, (char)(j+'A')});
                rows_d[j]--;
                cols_left--;
                cols_i[j]++;
                break;
            }
        }
    }

    std::cout << solution.size() << '\n';
    std::reverse(solution.begin(), solution.end());
    for (auto [a, b, c] : solution) std::cout << a << ' ' << b << ' ' << c << '\n';
}

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

    solve();

    return 0;
}