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

using namespace std;

typedef long long ll;


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

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

    vector<int> rows, columns;
    vector<pair<char, pair<int, char>>> commands;

    int rows_cnt[n + 1][26];
    int columns_cnt[m + 1][26];
    for (int i = 1; i <= n; i++) {
        for (int k = 0; k < 26; k++) {
            rows_cnt[i][k] = 0;
        }
    }
    for (int j = 1; j <= m; j++) {
        for (int k = 0; k < 26; k++) {
            columns_cnt[j][k] = 0;
        }
    }

    char color[n + 1][m + 1];
    for (int i = 1; i <= n; i++) {
        rows.push_back(i);
        for (int j = 1; j <= m; j++) {
            if (i == 1) {
                columns.push_back(j);
            }
            cin >> color[i][j];
            rows_cnt[i][int(color[i][j]) - int('A')]++;
            columns_cnt[j][int(color[i][j]) - int('A')]++;
        }
    }

    while (rows.size() > 0 && columns.size() > 0) {
        auto itr = rows.begin();
        while (rows_cnt[*itr][int(color[*itr][columns[0]]) - int('A')] != columns.size() && itr != rows.end()) {
            itr++;
        }
        if (itr != rows.end()) {
            if (rows_cnt[*itr][int(color[*itr][columns[0]]) - int('A')] == columns.size()) {
                pair<char, pair<int, char>> command;
                command.first = 'R';
                (command.second).first = *itr;
                (command.second).second = color[*itr][columns[0]];
                commands.push_back(command);
                for (int j = 0; j < columns.size(); j++) {
                    columns_cnt[columns[j]][int(color[*itr][columns[0]]) - int('A')]--;
                }
                rows.erase(itr);
            }
        }
        else {
            auto itc = columns.begin(); 
            while (columns_cnt[*itc][int(color[rows[0]][*itc]) - int('A')] != rows.size() && itc != columns.end()) {
                itc++;
            }

            pair<char, pair<int, char>> command;
            command.first = 'K';
            (command.second).first = *itc;
            (command.second).second = color[rows[0]][*itc];
            commands.push_back(command);
            for (int i = 0; i < rows.size(); i++) {
                rows_cnt[rows[i]][int(color[rows[0]][*itc]) - int('A')]--;
            }
            columns.erase(itc);
        }
    }

    cout << commands.size() << "\n";
    for (int i = commands.size() - 1; i >= 0; i--) {
        auto p = commands[i];
        cout << p.first << " " << (p.second).first << " " << (p.second).second << "\n";
    }

    return 0;
}