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 <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int N, M;
	cin >> N >> M;
	vector<string> goal(N);
	for(string& s : goal) cin >> s;
	vector<vector<int> > done(N, vector<int>(M));
	vector<vector<int> > row_freq(N, vector<int>(26));
	vector<vector<int> > col_freq(M, vector<int>(26));
	vector<int> row_nonzero(N, 0);
	vector<int> col_nonzero(M, 0);
	vector<int> row_done(N, 0);
	vector<int> col_done(M, 0);
	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			row_freq[i][goal[i][j]-'A']++;
			col_freq[j][goal[i][j]-'A']++;
		}
	}	
	vector<tuple<char, int, int> > ans;
	for(int i = 0; i < N; i++){
		int seen = -1;
		for(int c = 0; c < 26; c++){
			if(row_freq[i][c]){
				row_nonzero[i]++;
				seen = c;
			}
		}
		if(row_nonzero[i] <= 1){
			row_done[i] = 1;
			if(seen >= 0) ans.push_back({'R', i, seen});
		}
	}
	for(int i = 0; i < M; i++){
		int seen = -1;
		for(int c = 0; c < 26; c++){
			if(col_freq[i][c]){
				col_nonzero[i]++;
				seen = c;
			}
		}
		if(col_nonzero[i] <= 1){
			col_done[i] = 1;
			if(seen >= 0) ans.push_back({'K', i, seen});
		}
	}
	int idx = 0;
	while(idx < ans.size()){
		auto del = [&](int i, int j){
			int color = goal[i][j] - 'A';
			if(!done[i][j]){
				done[i][j] = 1;
				row_freq[i][color]--;
				if(row_freq[i][color] == 0) row_nonzero[i]--;
				if(row_nonzero[i] == 1 && !row_done[i]){
					row_done[i] = 1;
					int seen = -1;
					for(int c = 0; c < 26; c++) if(row_freq[i][c]) seen = c;
					if(seen >= 0) ans.push_back({'R', i, seen});
				}
				col_freq[j][color]--;
				if(col_freq[j][color] == 0) col_nonzero[j]--;
				if(col_nonzero[j] == 1 && !col_done[j]){
					col_done[j] = 1;
					int seen = -1;
					for(int c = 0; c < 26; c++) if(col_freq[j][c]) seen = c;
					if(seen >= 0) ans.push_back({'K', j, seen});
				}
			}
		};
		auto [type, x, c] = ans[idx];
		idx++;
		if(type == 'R'){
			for(int j = 0; j < M; j++){
				del(x, j);
			}
		} else if(type == 'K'){
			for(int i = 0; i < N; i++){
				del(i, x);
			}
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans.size() << '\n';
	for(auto [type, x, c] : ans){
		cout << type << ' ' << (x+1) << ' ' << char('A' + c) << '\n';
	}
}