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

vector<map<char, int>> vm;
vector<pair<int, char>> ans;
void dec(int j, char c) {
    auto it = vm[j].find(c);
    if(it == vm[j].end()) return;
    if(--it->second == 0) vm[j].erase(it);
    if(vm[j].size() == 1) {
        ans.push_back({j, vm[j].begin()->first});
        vm[j].clear();
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    vm.resize(n + m);
    ans.reserve(n + m);
    for(int i = 0; i < n; ++i) {
        cin >> v[i];
        for(int j = 0; j < m; ++j) {
            ++vm[i][v[i][j]];
            ++vm[n + j][v[i][j]];
        }
    }
    for(int j = 0; j < n + m; ++j)
        if(vm[j].size() == 1) {
            ans.push_back({j, vm[j].begin()->first});
            vm[j].clear();
        }

    for(int t = 0; t < ans.size(); ++t) {
        auto &&[tt, cc] = ans[t];
        if(tt < n)
            for(int j = n; j < n + m; ++j) dec(j, cc);
        else
            for(int i = 0; i < n; ++i) dec(i, cc);
    }
    cout << m + n << '\n';
    for(int i = m + n - 1; i >= 0; --i) {
        if(ans[i].first < n) {
            cout << "R " << ans[i].first + 1 << ' ' << ans[i].second << '\n';
        } else {
            cout << "K " << ans[i].first + 1 - n << ' ' << ans[i].second << '\n';
        }
    }
}