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
91
92
93
94
95
#include <bits/stdc++.h>

using namespace std;

pair<bool, char> monocolor(const array<int, 26>& a) {
    char color = 'A' - 1;
    for (int c = 0; c < 26; ++c) {
        if (a[c] == 0) continue;
        if (color >= 'A') return {false, color};
        color = 'A' + c;
    }
    return {color != 'A' - 1, color};
}

pair<list<pair<int, array<int, 26>>>::iterator, char> find_monocolor(list<pair<int, array<int, 26>>>& xs) {
    auto it = xs.begin();
    while (it != xs.end()) {
        auto [i, row] = *it;
        auto [is_monocolor, color] = monocolor(row);
        if (is_monocolor)
            return {it, color};
        ++it;
    }
    return {it, 'A' - 1};
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    vector<vector<char>> plansza(n, vector<char>(m));
    for (int i = 0; i < n; ++i) {
        string row;
        cin >> row;
        for (int j = 0; j < m; ++j) {
            plansza[i][j] = row[j];
        }
    }

    list<pair<int, array<int, 26>>> rows;
    list<pair<int, array<int, 26>>> cols;
    for (int i = 0; i < n; ++i) {
        array<int, 26> row = {};
        for (int j = 0; j < m; ++j) {
            ++row[plansza[i][j] - 'A'];
        }
        rows.push_back({i, row});
    }
    for (int j = 0; j < m; ++j) {
        array<int, 26> col = {};
        for (int i = 0; i < n; ++i) {
            ++col[plansza[i][j] - 'A'];
        }
        cols.push_back({j, col});
    }

    stack<pair<pair<char, int>, char>> res;

    for (int step = 0; step < n + m; ++step) {
       auto it = rows.begin();
        while (it != rows.end()) {
            auto [is_monocolor, color] = monocolor(it->second);
            if (is_monocolor) {
                for (auto& [j, col] : cols) --col[color - 'A'];
                res.push({{'R', it->first + 1}, color});
                it = rows.erase(it);
            } else {
                ++it;
            }
        }

        it = cols.begin();
        while (it != cols.end()) {
            auto [is_monocolor, color] = monocolor(it->second);
            if (is_monocolor) {
                for (auto& [i, row] : rows) --row[color - 'A'];
                res.push({{'K', it->first + 1}, color});
                it = cols.erase(it);
            } else {
                ++it;
            }
        }
    }

    cout << res.size() << "\n";
    while (!res.empty()) {
        auto [xy, color] = res.top();
        res.pop();
        cout << xy.first << " " << xy.second << " " << color << "\n";
    }

    return 0;
}