// Jan Zakrzewski #include <bits/stdc++.h> using namespace std; constexpr int N = 2024; constexpr int Z = 300; int n, m; char a[N][N]; int row_colors[N][Z]; int col_colors[N][Z]; bool row_visited[N]; bool col_visited[N]; vector<tuple<char,int,char>> answer; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; ++i) { for(int c = 'A'; c <= 'Z'; ++c) row_colors[i][c] = 0; row_visited[i] = false; } for(int j = 1; j <= m; ++j) { for(int c = 'A'; c <= 'Z'; ++c) col_colors[j][c] = 0; col_visited[j] = false; } for(int i = 1; i <= n; ++i) { string s; cin >> s; for(int j = 1; j <= m; ++j) { a[i][j] = s[j - 1]; ++row_colors[i][(int)a[i][j]]; ++col_colors[j][(int)a[i][j]]; } } answer.clear(); bool okay; do { okay = false; for(int i = 1; i <= n; ++i) { if(row_visited[i]) continue; char nonzero = '.'; for(int c = 'A'; c <= 'Z'; ++c) if(row_colors[i][c] > 0) { if(nonzero == '.') nonzero = c; else nonzero = '*'; } if(nonzero == '.') continue; if(nonzero == '*') continue; row_visited[i] = true; answer.push_back({'R', i, nonzero}); okay = true; for(int j = 1; j <= m; ++j) { if(col_visited[j]) continue; --col_colors[j][(int)nonzero]; } break; } for(int j = 1; j <= m; ++j) { if(col_visited[j]) continue; char nonzero = '.'; for(int c = 'A'; c <= 'Z'; ++c) if(col_colors[j][c] > 0) { if(nonzero == '.') nonzero = c; else nonzero = '*'; } if(nonzero == '.') continue; if(nonzero == '*') continue; col_visited[j] = true; answer.push_back({'K', j, nonzero}); okay = true; for(int i = 1; i <= n; ++i) { if(row_visited[i]) continue; --row_colors[i][(int)nonzero]; } break; } } while(okay); reverse(answer.begin(), answer.end()); cout << (int) answer.size() << "\n"; for(tuple<char,int,char> elm : answer) { cout << get<0>(elm) << " " << get<1>(elm) << " " << get<2>(elm) << "\n"; } return 0; }
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 | // Jan Zakrzewski #include <bits/stdc++.h> using namespace std; constexpr int N = 2024; constexpr int Z = 300; int n, m; char a[N][N]; int row_colors[N][Z]; int col_colors[N][Z]; bool row_visited[N]; bool col_visited[N]; vector<tuple<char,int,char>> answer; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; ++i) { for(int c = 'A'; c <= 'Z'; ++c) row_colors[i][c] = 0; row_visited[i] = false; } for(int j = 1; j <= m; ++j) { for(int c = 'A'; c <= 'Z'; ++c) col_colors[j][c] = 0; col_visited[j] = false; } for(int i = 1; i <= n; ++i) { string s; cin >> s; for(int j = 1; j <= m; ++j) { a[i][j] = s[j - 1]; ++row_colors[i][(int)a[i][j]]; ++col_colors[j][(int)a[i][j]]; } } answer.clear(); bool okay; do { okay = false; for(int i = 1; i <= n; ++i) { if(row_visited[i]) continue; char nonzero = '.'; for(int c = 'A'; c <= 'Z'; ++c) if(row_colors[i][c] > 0) { if(nonzero == '.') nonzero = c; else nonzero = '*'; } if(nonzero == '.') continue; if(nonzero == '*') continue; row_visited[i] = true; answer.push_back({'R', i, nonzero}); okay = true; for(int j = 1; j <= m; ++j) { if(col_visited[j]) continue; --col_colors[j][(int)nonzero]; } break; } for(int j = 1; j <= m; ++j) { if(col_visited[j]) continue; char nonzero = '.'; for(int c = 'A'; c <= 'Z'; ++c) if(col_colors[j][c] > 0) { if(nonzero == '.') nonzero = c; else nonzero = '*'; } if(nonzero == '.') continue; if(nonzero == '*') continue; col_visited[j] = true; answer.push_back({'K', j, nonzero}); okay = true; for(int i = 1; i <= n; ++i) { if(row_visited[i]) continue; --row_colors[i][(int)nonzero]; } break; } } while(okay); reverse(answer.begin(), answer.end()); cout << (int) answer.size() << "\n"; for(tuple<char,int,char> elm : answer) { cout << get<0>(elm) << " " << get<1>(elm) << " " << get<2>(elm) << "\n"; } return 0; } |