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
#include <iostream>
#include <vector>
#include <map>
#include <string>

// A function to count the occurrences of characters in a given vector of chars.
std::map<char, int> countOccurrences(const std::vector<char>& vec) {
    std::map<char, int> counter;
    for (char elem : vec) {
        ++counter[elem];
    }
    return counter;
}

// A function to find the most common element in a map (similar to Counter's most_common in Python).
char findMostCommon(const std::map<char, int>& counter) {
    int maxCount = 0;
    char mostCommon = '#'; // Initialize with a placeholder.
    for (const auto& pair : counter) {
        if (pair.second > maxCount) {
            maxCount = pair.second;
            mostCommon = pair.first;
        }
    }
    return mostCommon;
}

int main() {
    int r, c;
    std::cin >> r >> c;
    std::cin.ignore(); // To ignore the newline after reading numbers.

    std::vector<std::vector<char> > m(r, std::vector<char>(c));
    std::vector<std::map<char, int> > rows(r), cols(c);
    std::vector<std::string> res;
    int left = r * c;

    // Reading the matrix and initializing rows and cols counters.
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            std::cin >> m[i][j];
            rows[i][m[i][j]]++;
            cols[j][m[i][j]]++;
        }
    }

    while (left > 0) {
        for (int i = 0; i < r; ++i) {
            if (rows[i].size() != 1) continue;
            char color = findMostCommon(rows[i]);
            res.push_back("R " + std::to_string(i + 1) + " " + color);
            rows[i].clear(); // Equivalent to pop in Python's Counter for the specific color.

            for (int j = 0; j < c; ++j) {
                if (m[i][j] != '#') {
                    left--;
                    cols[j][m[i][j]]--;
                    if (cols[j][m[i][j]] == 0) {
                        cols[j].erase(m[i][j]);
                    }
                    m[i][j] = '#';
                }
            }
        }

        for (int j = 0; j < c; ++j) {
            if (cols[j].size() != 1) continue;
            char color = findMostCommon(cols[j]);
            res.push_back("K " + std::to_string(j + 1) + " " + color);
            cols[j].clear(); // Similar clearing as for rows.

            for (int i = 0; i < r; ++i) {
                if (m[i][j] != '#') {
                    left--;
                    rows[i][m[i][j]]--;
                    if (rows[i][m[i][j]] == 0) {
                        rows[i].erase(m[i][j]);
                    }
                    m[i][j] = '#';
                }
            }
        }
    }

    std::cout << res.size() << std::endl;
    for (auto it = res.rbegin(); it != res.rend(); ++it) {
        std::cout << *it << std::endl;
    }

    return 0;
}