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
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    vector<string> P(n);
    vector<map<char, int>> K0(m);
    vector<map<char, int>> W0(n);

    for (int w = 0; w < n; ++w) {
    	cin >> P[w];
        for (int k = 0; k < m; ++k) {
            char z = P[w][k];
            K0[k][z]++;
            W0[w][z]++;
        }
    }

    vector<int> K(m);
    vector<int> W(n);
    for (int i = 0; i < m; ++i) {
        K[i] = i;
    }
    for (int i = 0; i < n; ++i) {
        W[i] = i;
    }

    vector<string> ans;

    while (!K.empty() || !W.empty()) {
        vector<int> nextK;
        for (int k : K) {
            if (K0[k].size() > 1) {
                nextK.push_back(k);
            } else if (K0[k].size() == 1) {
                char c = K0[k].begin()->first;
                ans.push_back("K " + to_string(k + 1) + " " + string(1, c));
                for (int w : W) {
                    if (P[w][k] == c) {
                        P[w][k] = '@';
                        W0[w][c]--;
                        if (W0[w][c] == 0) {
                            W0[w].erase(c);
                        }
                    }
                }
            }
        }
        K = nextK;

        vector<int> nextW;
        for (int w : W) {
            if (W0[w].size() > 1) {
                nextW.push_back(w);
            } else if (W0[w].size() == 1) {
                char c = W0[w].begin()->first;
                ans.push_back("R " + to_string(w + 1) + " " + string(1, c));
                for (int k : K) {
                    if (P[w][k] == c) {
                        P[w][k] = '@';
                        K0[k][c]--;
                        if (K0[k][c] == 0) {
                            K0[k].erase(c);
                        }
                    }
                }
            }
        }
        W = nextW;
    }

    cout << ans.size() << endl;
    for (int i = ans.size() - 1; i >= 0; --i) {
        cout << ans[i] << endl;
    }

    return 0;
}