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

#define MAX_N  2007
#define mp make_pair

typedef long long ll;

using namespace std;

int n, m;
queue<pair<int, char>> nocolor;
stack<pair<int, char>> res;
set<char> ccounter[MAX_N], rcounter[MAX_N];
int cvec[MAX_N][100], rvec[MAX_N][100];
char plain[MAX_N][MAX_N];
char c;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> c;
            plain[i][j] = c;
            cvec[j][c]++;
            if (cvec[j][c] == 1) {
                ccounter[j].insert(c);
            }

            rvec[i][c]++;
            if (rvec[i][c] == 1) {
                rcounter[i].insert(c);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (rcounter[i].size() == 1) {
            nocolor.push(mp(i, *(rcounter[i].begin())));
        }
    }
    for (int i = 1; i <= m; i++) {
        if (ccounter[i].size() == 1) {
            nocolor.push(mp(-i, *(ccounter[i].begin())));
        }
    }
    while(!nocolor.empty()) {
        auto cur = nocolor.front();
        nocolor.pop();
        bool is_column = (cur.first < 0);
        res.push(cur);
        int temp = abs(cur.first);
        if (is_column) {
            for (int i = 1; i <= n; i++) {
                c = plain[i][temp];
                rvec[i][c]--;
                // cout << "r " << i << " " << c << " " << rvec[i][c] << "\n";
                if (rvec[i][c] == 0) {
                    rcounter[i].erase(c);
                    if (rcounter[i].size() == 1) {
                        nocolor.push(mp(i, *(rcounter[i].begin())));
                        rcounter[i].clear();
                    }
                }
                
            }
        } else {
            for (int i = 1; i <= m; i++) {
                c = plain[temp][i];
                cvec[i][c]--;
                // cout << "c " << i << " " << c << " " << cvec[i][c] << "\n";
                if (cvec[i][c] == 0) {
                    ccounter[i].erase(c);
                    if (ccounter[i].size() == 1) {
                        nocolor.push(mp(-i, *(ccounter[i].begin())));
                        ccounter[i].clear();
                    }
                }
            }
        }

    }

    cout << res.size() << "\n";

    while(!res.empty()) {
        auto cur = res.top();
        res.pop();
        bool is_column = (cur.first < 0);
        string ck = is_column ? "K " : "R ";
        cout << ck << abs(cur.first) << " " << cur.second << "\n";
    }
}