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

const int lim = 2'003;
int tab[lim][lim];
int kolor[30];
parii col[lim];
parii wie[lim];
queue < pair < parii, int > > kolej;
vector < parii > v;
vector < parii > w;

int skipped_k = 0;
int skipped_r = 0;

int main(){
    int n, m;
    cin >> n >> m;
    for(int  i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            char x;
            cin >> x;
            tab[i][j] = x - 'A' + 1;
            cout << tab[i][j] << " ";
        }
        cout << "\n";
    }

    for(int  i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            kolor[tab[i][j]]++;
        }
        int akt = 0, ni = 0;
        for(int j = 1; j < 28; j++){
            if(kolor[j] > akt){
                akt = kolor[j];
                ni = j;
            }
            v.push_back(make_pair(kolor[j], j));
            if(kolor[j] != 0){
                v.push_back(make_pair(kolor[j], j));
                kolor[j] = 0;
            }
        }
        wie[i] = make_pair(akt, ni);
        if(wie[i].first == n){
            skipped_r++;
            kolej.push(make_pair(make_pair(1, i), ni));
        }
        //sort(v[i].begin(), v[i].end(), greater < pair < int, int > >());
        //cout << akt << " " << ni << "\n";
    }
    
    for(int  i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            kolor[tab[j][i]]++;
        }
        int akt = 0, ni = 0;
        for(int j = 1; j < 28; j++){
            if(kolor[j] > akt){
                akt = kolor[j];
                ni = j;
            }
            if(kolor[j] != 0){
                w.push_back(make_pair(kolor[j], j));
                kolor[j] = 0;
            }
        }
        col[i] = make_pair(akt, ni);
        if(col[i].first == m){
            skipped_k++;
            kolej.push(make_pair(make_pair(2, i), ni));
        }
        //sort(w[i].begin(), w[i].end(), greater < pair < int, int > >());
        //cout << akt << " " << ni << "\n";
    }
    sort(v.begin(), v.end(), greater< parii>());
    sort(w.begin(), w.end(), greater<parii>());
    int id_k = 0;
    int id_r = 0;
    while(!kolej.empty()){
        int rodzaj = kolej.front().first.first, numer = kolej.front().first.second, zm = kolej.front().second;
        kolej.pop();
        if(rodzaj == 1){
            skipped_r++;
            cout << "R ";
            if(id_k < w.size()){
                int i = 0;
                while(id_k + i < w.size() && w[id_k + i].second == zm){
                    i++;
                }
                if(w[id_k + i].first >= m - skipped_r){
                    //cout << w[id_k + i].first << "  ";
                    id_k ++;
                    kolej.push(make_pair(make_pair(2, numer), w[id_k + i].second));
                }
            }
        }
        else {
            skipped_k++;
            cout << "K ";
            if(id_r < v.size()){
                int i = 0;
                while(id_r + i < v.size() && v[id_r + i].second == zm){
                    i++;
                }
                if(v[id_r + i].first >= n - skipped_k){
                    //cout << v[id_r + i].first << "  ";
                    id_r++;
                    kolej.push(make_pair(make_pair(1, numer), v[id_r + i].second));
                }
            }
        }
        cout << numer + 1 << " ";
        cout << char(zm + 'A' - 1) << "\n";
    }
}