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
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int S='Z'-'A'+1;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m; cin>>n>>m;
	vector t(n+1, vector(m+1, (char)(0)));
	vector<array<int, S>>w(n+1), k(m+1);
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++)
		{
			cin>>t[i][j];
			t[i][j]-='A';
			w[i][t[i][j]]++;
			k[j][t[i][j]]++;
		}
	int currn=n, currm=m;
	queue<pair<int, int>>kol1, kol2;
	for (int i=1; i<=n; i++)
		for (int j=0; j<S; j++)
			if (w[i][j] == currm)
			{
				w[i][j]=-1;
				kol2.push({i, j});
			}
	for (int i=1; i<=m; i++)
		for (int j=0; j<S; j++)
			if (k[i][j] == currn)
			{
				k[i][j]=-1;
				kol1.push({i, j});
			}
	vector<pair<int, int>>ans;
	while (currn && currm)
	{
		if (!kol1.empty())
		{
			int a=kol1.front().f, b=kol1.front().s;
			//~ cout<<"XD "<<a<<" "<<b<<endl;
			kol1.pop();
			ans.push_back({a, b});
			currm--;
			for (int i=1; i<=n; i++)
				if (t[i][a]==b)
					{
						t[i][a]=-1;
						w[i][b]--;
						for (int j=0; j<S; j++)
							if (w[i][j] == currm)
							{
								kol2.push({i, j});
								w[i][j]=-1;
							}
					}
		}
		else
		{
			
			int a=kol2.front().f, b=kol2.front().s;
			kol2.pop();
			//~ cout<<"XD2 "<<a<<" "<<b<<endl;
			ans.push_back({-a, b});
			currn--;
			for (int i=1; i<=m; i++)
				if (t[a][i]==b)
					{
						t[a][i]=-1;
						k[i][b]--;
						for (int j=0; j<S; j++)
							if (k[i][j] == currn)
							{
								kol1.push({i, j});
								k[i][j]=-1;
							}
					}
		}
	}
	cout<<ans.size()<<"\n";
	for (int i=ans.size()-1; i>=0; i--)
		cout<<(ans[i].f<0? "R " : "K ")<<abs(ans[i].f)<<" "<<(char)(ans[i].s+'A')<<"\n";
}