#include <bits/stdc++.h> using namespace std; #define mp(a,b) make_pair(a,b) #define ff first #define setp(a) setprecision(a)<<fixed #define ss second #define fori(v) for(int i=0; i<v; i++) #define forj(v) for(int j=0; j<v; j++) #define fork(v) for(int k=0; k<v; k++) #define forl(v) for(int l=0; l<v; l++) #define fort(v) for(int t=0; t<v; t++) #define forz(v) for(int z=0; z<v; z++) #define forx(v) for(int x=0; x<v; x++) #define fory(v) for(int y=0; y<v; y++) #define ll long long #define lll __int128 #define ld long double #define pb(a) push_back(a) #define mt make_tuple // #define cout out // #define cin in ll inf = (ll)pow(10, 9); ll modulo = 998244353; ld eps = 1e-13; ifstream in; ofstream out; void deal(){ ll n, m; cin>>n>>m; vector< vector<int> > rows(n, vector<int>(26, 0)), cols(m, vector<int>(26, 0)); vector<vector<char> > arr(n, vector<char>(m)); vector<bool> selR(n, 0), selC(m, 0); fori(n){ forj(m){ cin>>arr[i][j]; } } fori(n){ forj(m){ rows[i][arr[i][j] - 'A']++; cols[j][arr[i][j] - 'A']++; } } ll totr = n, totc = m; vector<tuple<char, int, char> > ans; while(totr && totc){ fori(n){ if(!selR[i]){ ll mx = 0; forj(26){ if(rows[i][j] > rows[i][mx]){ mx = j; } } if(rows[i][mx] == totc){ ans.pb(mt('R', i+1, 'A' + mx)); forj(m){ char f = arr[i][j]; cols[j][f-'A']--; } --totr; selR[i] = 1; } } } forj(m){ if(!selC[j]){ ll mx = 0; fork(26){ if(cols[j][k] > cols[j][mx]){ mx = k; } } if(cols[j][mx] == totr){ ans.pb(mt('K', j+1, 'A' + mx)); fori(n){ char f = arr[i][j]; rows[i][f-'A']--; } --totc; selC[j] = 1; } } } } reverse(ans.begin(), ans.end()); cout<<ans.size()<<'\n'; for(auto& el : ans){ auto [v1, v2, v3] = el; cout<<v1<<" "<<v2<<" "<<v3<<'\n'; } } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); deal(); }
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 | #include <bits/stdc++.h> using namespace std; #define mp(a,b) make_pair(a,b) #define ff first #define setp(a) setprecision(a)<<fixed #define ss second #define fori(v) for(int i=0; i<v; i++) #define forj(v) for(int j=0; j<v; j++) #define fork(v) for(int k=0; k<v; k++) #define forl(v) for(int l=0; l<v; l++) #define fort(v) for(int t=0; t<v; t++) #define forz(v) for(int z=0; z<v; z++) #define forx(v) for(int x=0; x<v; x++) #define fory(v) for(int y=0; y<v; y++) #define ll long long #define lll __int128 #define ld long double #define pb(a) push_back(a) #define mt make_tuple // #define cout out // #define cin in ll inf = (ll)pow(10, 9); ll modulo = 998244353; ld eps = 1e-13; ifstream in; ofstream out; void deal(){ ll n, m; cin>>n>>m; vector< vector<int> > rows(n, vector<int>(26, 0)), cols(m, vector<int>(26, 0)); vector<vector<char> > arr(n, vector<char>(m)); vector<bool> selR(n, 0), selC(m, 0); fori(n){ forj(m){ cin>>arr[i][j]; } } fori(n){ forj(m){ rows[i][arr[i][j] - 'A']++; cols[j][arr[i][j] - 'A']++; } } ll totr = n, totc = m; vector<tuple<char, int, char> > ans; while(totr && totc){ fori(n){ if(!selR[i]){ ll mx = 0; forj(26){ if(rows[i][j] > rows[i][mx]){ mx = j; } } if(rows[i][mx] == totc){ ans.pb(mt('R', i+1, 'A' + mx)); forj(m){ char f = arr[i][j]; cols[j][f-'A']--; } --totr; selR[i] = 1; } } } forj(m){ if(!selC[j]){ ll mx = 0; fork(26){ if(cols[j][k] > cols[j][mx]){ mx = k; } } if(cols[j][mx] == totr){ ans.pb(mt('K', j+1, 'A' + mx)); fori(n){ char f = arr[i][j]; rows[i][f-'A']--; } --totc; selC[j] = 1; } } } } reverse(ans.begin(), ans.end()); cout<<ans.size()<<'\n'; for(auto& el : ans){ auto [v1, v2, v3] = el; cout<<v1<<" "<<v2<<" "<<v3<<'\n'; } } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); deal(); } |