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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n, m; cin >> n >> m;
    vector<vector<int>> target(n, vector<int>());
    const int LETTERS = 26;
    vector<vector<int>> row_cnts(n, vector<int>(LETTERS, 0));
    vector<vector<int>> col_cnts(m, vector<int>(LETTERS, 0));
    char c;
    int letter_code;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            cin >> c;
            letter_code = (int) c - 65;
            target[i].push_back(letter_code);
            ++row_cnts[i][letter_code];
            ++col_cnts[j][letter_code];
        }
    }

    vector<int> r_colors(n, 0);
    vector<int> c_colors(m, 0);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < LETTERS; ++j) {
            if(row_cnts[i][j] > 0) {
                ++r_colors[i];
            }
        }
    }
    for(int i = 0; i < m; ++i) {
        for(int j = 0; j < LETTERS; ++j) {
            if(col_cnts[i][j] > 0) {
                ++c_colors[i];
            }
        }
    }

    stack<pair<pair<char, int>, char>> moves;
    // Get all the initially available moves
    queue<pair<char, int>> move_queue;
    for(int i = 0; i < n; ++i) {
        if(r_colors[i] == 1) {
            move_queue.push(make_pair('R', i));
        }
    }
    for(int i = 0; i < m; ++i) {
        if(c_colors[i] == 1) {
            move_queue.push(make_pair('K', i));
        }
    }

    // Process the moves
    pair<char, int> move;
    int move_line;
    int wanted_color;
    while(!move_queue.empty()) {
        move = move_queue.front();
        move_queue.pop();
        move_line = move.second;
        if(move.first == 'R') {
            if(r_colors[move_line] == 0) continue;
            r_colors[move_line] = 0;
            for(int i = 0; i < LETTERS; ++i) {
                if(row_cnts[move_line][i] > 0) {
                    wanted_color = i;
                    break;
                }
            }
            moves.push(make_pair(move, (char) (wanted_color + 65)));
            for(int i = 0; i < m; ++i) {
                if(c_colors[i] != 0) {
                    if(--col_cnts[i][wanted_color] <= 0) {
                        if(--c_colors[i] <= 1) {
                            move_queue.push(make_pair('K', i));
                        }
                    }
                }
            }
        }
        if(move.first == 'K') {
            if(c_colors[move_line] == 0) continue;
            c_colors[move_line] = 0;
            for(int i = 0; i < LETTERS; ++i) {
                if(col_cnts[move_line][i] > 0) {
                    wanted_color = i;
                    break;
                }
            }
            moves.push(make_pair(move, (char) (wanted_color + 65)));
            for(int i = 0; i < n; ++i) {
                if(r_colors[i] != 0) {
                    if(--row_cnts[i][wanted_color] <= 0) {
                        if(--r_colors[i] <= 1) {
                            move_queue.push(make_pair('R', i));
                        }
                    }
                }
            }
        }
    }

    pair<pair<char, int>, char> full_move;
    cout << moves.size() << "\n";
    while(!moves.empty()) {
        full_move = moves.top();
        moves.pop();
        cout << full_move.first.first << " " << full_move.first.second + 1 << " " << full_move.second << "\n";
    }

    return 0;
}