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

#define PI pair<int, int>

using namespace std;

PI find_row(int n, int m, const vector<vector<int>> &cnt, int rows, int columns)
{
    for (int i = 0; i < n; ++i)
    {
        for (int c = 0; c < 26; ++c)
        {
            if (cnt[c][i] == columns) return {i, c};
        }
    }

    for (int i = n; i < n + m; ++i)
    {
        for (int c = 0; c < 26; ++c)
        {
            if (cnt[c][i] == rows) return {i, c};
        }
    }

    return {-1, -1};
}

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

    int n, m;
    cin >> n >> m;

    vector<vector<int>> cnt(26, vector<int>(n + m));
    vector<PI> moves;

    string row;
    for (int i = 0; i < n; ++i)
    {
        cin >> row;
        for (int j = 0; j < m; ++j)
        {
            ++cnt[row[j] - 'A'][i];
            ++cnt[row[j] - 'A'][n + j];
        }
    }

    int squares = n * m;
    int rows = n;
    int columns = m;

    while (squares)
    {
        auto [x, c] = find_row(n, m, cnt, rows, columns);
        cnt[c][x] = 0;
        moves.emplace_back(x, c);

        if (x < n)
        {
            squares -= columns;
            --rows;
            for (int i = n; i < n + m; ++i) --cnt[c][i];
        }
        else
        {
            squares -= rows;
            --columns;
            for (int i = 0; i < n; ++i) --cnt[c][i];
        }
    }

    cout << moves.size() << "\n";
    for (auto itr = moves.rbegin(); itr != moves.rend(); ++itr)
    {
        auto &[r, c] = *itr;
        if (r < n) cout << "R " << r + 1 << " " << (char)('A' + c) << "\n";
        else cout << "K " << r - n + 1 << " " << (char)('A' + c) << "\n";
    }
}