#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <utility>
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	int n,m;
	cin >> n >> m;
    vector<vector<string>> tab;
	vector<pair<string, pair<int, string>>> wynik;
	vector<map<string,int>> kol;
	for (int i=0; i<m; i++) 
		kol.push_back({});
	set<int> aktkol = {};
    for (int i=0; i<m; i++)
        aktkol.insert(i);
	vector<map<string,int>> wier;
	for (int i=0; i<n; i++) 
		wier.push_back({});
	set<int> aktwier = {};
    for (int i=0; i<n; i++)
        aktwier.insert(i);
    vector<pair<int,string>> pustek = {};
    vector<pair<int,string>> pustew = {};
	string s;
    for (int i=0; i<n; i++) {
		tab.push_back({});
		cin >> s;
	    for (int j=0; j<m; j++) {
			tab[i].push_back(s.substr(j,1));
		}
	}
	
	string k;
	for (int wn=0; wn<tab.size(); wn++) {
		for (int num=0; num < tab[wn].size(); num++) {
			k = tab[wn][num];
			kol[num][k]+=1;
			wier[wn][k]+=1;
		}
	}
	for (int ind=0; ind < wier.size(); ind++) {
		if (wier[ind].size() == 1) {
			pustew.push_back(make_pair(ind,wier[ind].begin()->first));
		}
	}
	
	for (int ind=0; ind < kol.size(); ind++) {
		if (kol[ind].size() == 1) {
			pustek.push_back(make_pair(ind,kol[ind].begin()->first));
		}
	}
	
    pair<int,string> para;        
    while (pustek.size() || pustew.size()) {
        while (pustek.size()) {
			para = pustek[pustek.size()-1];
            wynik.push_back(make_pair("K", para));
			pustek.pop_back();
            aktkol.erase(para.first);
            for (auto ind: aktwier) {
                wier[ind][para.second] -= 1;
                if (wier[ind][para.second] == 0) {
                    wier[ind].erase(para.second);
                    if (wier[ind].size() == 1) {
                        pustew.push_back(make_pair(ind,wier[ind].begin()->first));
					}
				}
			}
		}
        while (pustew.size()) {
			para = pustew[pustew.size()-1];
            wynik.push_back(make_pair("R", para));
			pustew.pop_back();
            aktwier.erase(para.first);
            for (auto ind: aktkol) {
                kol[ind][para.second] -= 1;
                if (kol[ind][para.second] == 0) {
                    kol[ind].erase(para.second);
                    if (kol[ind].size() == 1) {
                        pustek.push_back(make_pair(ind,kol[ind].begin()->first));
					}
				}
			}
		}
	}
	cout << wynik.size() << "\n";
    while (wynik.size()) {
		cout << wynik[wynik.size()-1].first << " " <<
			wynik[wynik.size()-1].second.first+1 << " " <<
			wynik[wynik.size()-1].second.second << "\n";
		wynik.pop_back();
	}
}
        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  | #include <iostream> #include <vector> #include <set> #include <map> #include <utility> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin >> n >> m; vector<vector<string>> tab; vector<pair<string, pair<int, string>>> wynik; vector<map<string,int>> kol; for (int i=0; i<m; i++) kol.push_back({}); set<int> aktkol = {}; for (int i=0; i<m; i++) aktkol.insert(i); vector<map<string,int>> wier; for (int i=0; i<n; i++) wier.push_back({}); set<int> aktwier = {}; for (int i=0; i<n; i++) aktwier.insert(i); vector<pair<int,string>> pustek = {}; vector<pair<int,string>> pustew = {}; string s; for (int i=0; i<n; i++) { tab.push_back({}); cin >> s; for (int j=0; j<m; j++) { tab[i].push_back(s.substr(j,1)); } } string k; for (int wn=0; wn<tab.size(); wn++) { for (int num=0; num < tab[wn].size(); num++) { k = tab[wn][num]; kol[num][k]+=1; wier[wn][k]+=1; } } for (int ind=0; ind < wier.size(); ind++) { if (wier[ind].size() == 1) { pustew.push_back(make_pair(ind,wier[ind].begin()->first)); } } for (int ind=0; ind < kol.size(); ind++) { if (kol[ind].size() == 1) { pustek.push_back(make_pair(ind,kol[ind].begin()->first)); } } pair<int,string> para; while (pustek.size() || pustew.size()) { while (pustek.size()) { para = pustek[pustek.size()-1]; wynik.push_back(make_pair("K", para)); pustek.pop_back(); aktkol.erase(para.first); for (auto ind: aktwier) { wier[ind][para.second] -= 1; if (wier[ind][para.second] == 0) { wier[ind].erase(para.second); if (wier[ind].size() == 1) { pustew.push_back(make_pair(ind,wier[ind].begin()->first)); } } } } while (pustew.size()) { para = pustew[pustew.size()-1]; wynik.push_back(make_pair("R", para)); pustew.pop_back(); aktwier.erase(para.first); for (auto ind: aktkol) { kol[ind][para.second] -= 1; if (kol[ind][para.second] == 0) { kol[ind].erase(para.second); if (kol[ind].size() == 1) { pustek.push_back(make_pair(ind,kol[ind].begin()->first)); } } } } } cout << wynik.size() << "\n"; while (wynik.size()) { cout << wynik[wynik.size()-1].first << " " << wynik[wynik.size()-1].second.first+1 << " " << wynik[wynik.size()-1].second.second << "\n"; wynik.pop_back(); } }  | 
            
        
                    English