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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <vector>
#include <unordered_map>

void printGrid(const std::vector<std::vector<char>> &grid)
{
    for (const auto &row : grid)
    {
        for (const auto &cell : row)
        {
            std::clog << cell;
        }
        std::clog << std::endl;
    }
}

void printMap(std::string name, const std::unordered_map<long long, std::unordered_map<char, long long>> &map)
{
    std::clog << name << ":\n";
    for (const auto &pair : map)
    {
        std::clog << pair.first + 1 << "\t";
        for (const auto &pair2 : pair.second)
        {
            std::clog << pair2.first << " " << pair2.second << ", ";
        }
        std::clog << std::endl;
    }
}

struct Cell
{
    char type;
    long long number;
    char color;
};

std::vector<long long> getToDelete(std::unordered_map<long long, std::unordered_map<char, long long>> &map,
                             std::unordered_map<long long, std::unordered_map<char, long long>> &intersectingMap,
                             std::vector<Cell> &result,
                             char type)
{
    std::vector<long long> toDelete;
    for (const auto &item : map)
    {
        for (const auto &color : item.second)
        {
            if (color.second == intersectingMap.size())
            {
                //std::clog << type << " " << item.first+1 << " " << color.second << " " << color.first << std::endl;
                result.push_back({type, item.first, color.first});
                toDelete.push_back(item.first);
                for (auto it = intersectingMap.begin(); it != intersectingMap.end(); ++it)
                {
                    it->second[color.first] -= 1;
                    if (it->second[color.first] == 0)
                    {
                        it->second.erase(color.first);
                    }
                }
            }
        }
    }
    return toDelete;
}

std::vector<Cell> solve(long long n, long long m, std::vector<std::vector<char>> &grid)
{
    std::unordered_map<long long, std::unordered_map<char, long long>> rowFreq;
    for (long long i = 0; i < n; ++i)
    {
        for (long long j = 0; j < m; ++j)
        {
            ++rowFreq[i][grid[i][j]];
        }
    }

    std::unordered_map<long long, std::unordered_map<char, long long>> colFreq;
    for (long long j = 0; j < m; ++j)
    {
        for (long long i = 0; i < n; ++i)
        {
            ++colFreq[j][grid[i][j]];
        }
    }

    std::vector<Cell> result;
    result.reserve(n + m);

    long long rows = n;
    long long cols = m;
    while (!rowFreq.empty() && !colFreq.empty())
    {
        //printMap("rows", rowFreq);
        //printMap("cols", colFreq);

        std::vector<long long> rowsToDelete = getToDelete(rowFreq, colFreq, result, 'R');

        for(const auto &row : rowsToDelete) {
            rowFreq.erase(row);
        }

        std::vector<long long> colsToDelete = getToDelete(colFreq, rowFreq, result, 'K');

        for(const auto &col : colsToDelete) {
            colFreq.erase(col);
        }

        /*std::clog << "===\n";
        printMap("rows", rowFreq);
        printMap("cols", colFreq);*/
    }

    return result;
}

int main()
{
    long long n, m;
    std::cin >> n >> m;
    std::vector<std::vector<char>> grid(n, std::vector<char>(m));
    for (long long i = 0; i < n; ++i)
    {
        for (long long j = 0; j < m; ++j)
        {
            std::cin >> grid[i][j];
        }
    }

    //printGrid(grid);

    auto result = solve(n, m, grid);

    std::cout << result.size() << std::endl;
    for (auto it = result.rbegin(); it != result.rend(); ++it)
    {
        std::cout << it->type << " " << it->number + 1 << " " << it->color << std::endl;
    }

    return 0;
}