#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 2005; char t[N][N]; bool vis[2][N]; int row[26][N]; int col[26][N]; int main(){ BOOST; int n, m, ns, ms; cin >> n >> m; ns = n, ms = m; vector<pair<int, ii>> ans; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ cin >> t[i][j]; row[t[i][j] - 'A'][i]++; col[t[i][j] - 'A'][j]++; } } for(int i=0; i<n; i++){ for(int k=0; k<26; k++){ if(row[k][i] == ms && !vis[0][i]){ // deb(i), deb(k), deb(row[k][i]); // exit(0); vis[0][i] = 1; ans.pb({0, {i, k}}); } } } for(int j=0; j<m; j++){ for(int k=0; k<26; k++){ if(col[k][j] == ns && !vis[1][j]){ vis[1][j] = 1; ans.pb({1, {j, k}}); } } } // exit(0); for(int i=0; i<ans.size(); i++){ auto it = ans[i]; // printf("ans[i] : {%d, {%d, %d}}\n", it.fi, it.se.fi, it.se.se); auto x = ans[i]; if(x.fi == 0){ ns--; for(int j=0; j<m; j++){ col[t[x.se.fi][j] - 'A'][j]--; for(int k=0; k<26; k++){ if(col[k][j] == ns && !vis[1][j]){ vis[1][j] = 1; ans.pb({1, {j, k}}); // printf("pushed : {%d, {%d, %d}}\n", 1, j, k); } } } } else{ ms--; for(int j=0; j<n; j++){ row[t[j][x.se.fi] - 'A'][j]--; for(int k=0; k<26; k++){ if(row[k][j] == ms && !vis[0][j]){ // printf("ms: %d\n", ms); // printf("row[%d][%d]: %d\n", k, j, row[k][j]); vis[0][j] = 1; ans.pb({0, {j, k}}); // printf("pushed : {%d, {%d, %d}}\n", 0, j, k); } } } } } reverse(all(ans)); cout << ans.size() << "\n"; for(int i=0; i<ans.size(); i++){ if(ans[i].fi == 0) cout << "R "; else cout << "K "; cout << ans[i].se.fi + 1 << " " << (char)(ans[i].se.se + 'A') << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 2005; char t[N][N]; bool vis[2][N]; int row[26][N]; int col[26][N]; int main(){ BOOST; int n, m, ns, ms; cin >> n >> m; ns = n, ms = m; vector<pair<int, ii>> ans; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ cin >> t[i][j]; row[t[i][j] - 'A'][i]++; col[t[i][j] - 'A'][j]++; } } for(int i=0; i<n; i++){ for(int k=0; k<26; k++){ if(row[k][i] == ms && !vis[0][i]){ // deb(i), deb(k), deb(row[k][i]); // exit(0); vis[0][i] = 1; ans.pb({0, {i, k}}); } } } for(int j=0; j<m; j++){ for(int k=0; k<26; k++){ if(col[k][j] == ns && !vis[1][j]){ vis[1][j] = 1; ans.pb({1, {j, k}}); } } } // exit(0); for(int i=0; i<ans.size(); i++){ auto it = ans[i]; // printf("ans[i] : {%d, {%d, %d}}\n", it.fi, it.se.fi, it.se.se); auto x = ans[i]; if(x.fi == 0){ ns--; for(int j=0; j<m; j++){ col[t[x.se.fi][j] - 'A'][j]--; for(int k=0; k<26; k++){ if(col[k][j] == ns && !vis[1][j]){ vis[1][j] = 1; ans.pb({1, {j, k}}); // printf("pushed : {%d, {%d, %d}}\n", 1, j, k); } } } } else{ ms--; for(int j=0; j<n; j++){ row[t[j][x.se.fi] - 'A'][j]--; for(int k=0; k<26; k++){ if(row[k][j] == ms && !vis[0][j]){ // printf("ms: %d\n", ms); // printf("row[%d][%d]: %d\n", k, j, row[k][j]); vis[0][j] = 1; ans.pb({0, {j, k}}); // printf("pushed : {%d, {%d, %d}}\n", 0, j, k); } } } } } reverse(all(ans)); cout << ans.size() << "\n"; for(int i=0; i<ans.size(); i++){ if(ans[i].fi == 0) cout << "R "; else cout << "K "; cout << ans[i].se.fi + 1 << " " << (char)(ans[i].se.se + 'A') << "\n"; } } |