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

int C[2000][2000];
int row_counts[2000][26];
int col_counts[2000][26];

bool is_good(int *row) {
    int different_colors = 26 - std::count(row, row + 26, 0);
    return different_colors == 1;
}

int unique_color(int *row) {
    for(int i = 0; i < 26; i++) {
        if(row[i] != 0) {
            return i;
        }
    }
    assert(false);
    return -1;
}

int main() {
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            char c_ij;
            cin >> c_ij;
            C[i][j] = int(c_ij) - int('A');
            row_counts[i][C[i][j]]++;
            col_counts[j][C[i][j]]++;
        }
    }
    std::set<int> active_rows, active_cols;
    for(int i = 0; i < n; i++) {
        active_rows.insert(i);
    }
    for(int j = 0; j < m; j++) {
        active_cols.insert(j);
    }

    std::vector<std::tuple<char, int, int>> solution;
    
    //std::cerr << "HELLO\n";

    while(active_rows.size() > 0 and active_cols.size() > 0) {
        //std::cerr << "rows: ";
        //std::copy(active_rows.begin(), active_rows.end(), std::ostream_iterator<int>(std::cerr, " "));
        //std::cerr << "\ncols: ";
        //std::copy(active_cols.begin(), active_cols.end(), std::ostream_iterator<int>(std::cerr, " "));
        //std::cerr << "\n";

        //std::cerr << "HELLO\n";
        for(int row : active_rows) {
            if(is_good(row_counts[row])) {
                int row_color = unique_color(row_counts[row]);
                solution.push_back({'R', row, row_color});
                active_rows.erase(row);
                for(int col : active_cols) {
                    col_counts[col][row_color]--;
                }
                break;
            }
        }
        // same but for columns
        for(int col : active_cols) {
            if(is_good(col_counts[col])) {
                int col_color = unique_color(col_counts[col]);
                solution.push_back({'K', col, col_color});
                active_cols.erase(col);
                for(int row : active_rows) {
                    row_counts[row][col_color]--;
                }
                break;
            }
        }
    }

    //std::cerr << "not reversed!\n";
    std::cout << solution.size() << "\n";
    for(auto ptr = solution.rbegin(); ptr != solution.rend(); ptr++) {
        std::cout << std::get<0>(*ptr) << " " << std::get<1>(*ptr) + 1 << " " << char(std::get<2>(*ptr) + 'A') << "\n";
    }
}