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

using namespace std;

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int n, m; cin >> n >> m; 
	vector<vector<int>> kolk(m + 1), kolw(n + 1);
	vector<int> ilek(m + 1), ilew(n + 1);
	
	for(int i = 1; i <= m; ++i) kolk[i].resize(26);
	for(int i = 1; i <= n; ++i) kolw[i].resize(26);
	
	char W; int k;
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= m; ++j){
			cin >> W;
			k =  W - 'A';
			if(!kolw[i][k]) ++ilew[i];
			if(!kolk[j][k]) ++ilek[j];
			++kolw[i][k]; ++kolk[j][k];
		}
	}
	
	struct st{int typ, x, kol;}; queue<st> q; int p; vector<st> wyn;
	vector<int> odwk(m + 1), odww(n + 1); 

	for(int i = 1; i <= m; ++i){
		if(ilek[i] == 1){
			for(int j = 0; j < 26; ++j){
				if(kolk[i][j]){p = j; break;}
			}
			q.push({0,i, p}); odwk[i] = 1;
		 }
	}
	for(int i = 1; i <= n; ++i){
		if(ilew[i] == 1) {
			for(int j = 0; j < 26; ++j){
				if(kolw[i][j]){p = j; break;}
			}
			q.push({1,i,p}); odww[i] = 1;
		}
	}
	
	
	while(!q.empty()){
		int typ = q.front().typ; int x = q.front().x; int kol = q.front().kol; q.pop();
		wyn.push_back({typ,x,kol});
		if(!typ){
			odwk[x] = 1;
			for(int j = 1; j <= n; ++j){
				--kolw[j][kol]; if(!kolw[j][kol]) --ilew[j];
				if(ilew[j] == 1 && !odww[j]){
					for(int q = 0; q < 26; ++q){
						if(kolw[j][q]){p = q; break;}
					}
					odww[j] = 1; q.push({1,j,p});
				}
			}
		}
		else{
			odww[x] = 1;
			for(int i = 1; i <= m; ++i){
				--kolk[i][kol]; if(!kolk[i][kol]) --ilek[i];
				if(ilek[i] == 1 && !odwk[i]){
					for(int q = 0; q < 26; ++q){
						if(kolk[i][q]){p = q; break;}
					}
					odwk[i] = 1; q.push({0,i,p});
				}
			}
		}
	}
	
	cout << wyn.size() << "\n";
	for(int i = (int)wyn.size() - 1; ~i; --i){
		if(!wyn[i].typ) cout << "K ";
		else cout << "R ";
		cout << wyn[i].x << " " << (char)(wyn[i].kol + 'A');
		cout << "\n";
	}
	
	return 0;
}