#include <iostream> #include <vector> using namespace std; const int mx = 2002; const int alfabet = 26; int n, m; string s[mx]; int counter[2][mx][alfabet]; int num_uniques[2][mx]; bool visited[2][mx]; struct turn{ int direction; //row=0, column=1 int index; int color;//A=0, Z=25 }; vector<turn> q; int find_color(int direction, int index){ for(int i = 0; i < alfabet; ++i){ if(counter[direction][index][i] > 0){ return i; } } return 0; } int main(){ ios::sync_with_stdio(false); cin >> n >> m; for(int i = 0; i < n; ++i){ cin >> s[i]; for(int j = 0; j < m; ++j){ int color = s[i][j] - 'A'; if(counter[0][i][color] == 0){ ++num_uniques[0][i]; } ++counter[0][i][color]; if(counter[1][j][color] == 0){ ++num_uniques[1][j]; } ++counter[1][j][color]; } } for(int d = 0; d < 2; ++d){ int sz = (d == 0) ? n : m; for(int i = 0; i < sz; ++i){ if(num_uniques[d][i] == 1){ int c = find_color(d, i); q.push_back({d, i, c}); visited[d][i] = true; } } } for(int i = 0; i < q.size(); ++i){ int direction = q[i].direction; int index = q[i].index; int color = q[i].color; if(direction == 0){ for(int j = 0; j < m; ++j){ int color = s[index][j] - 'A'; if(counter[0][index][color] == 1){ --num_uniques[0][index]; } --counter[0][index][color]; if(counter[1][j][color] == 1){ --num_uniques[1][j]; } --counter[1][j][color]; if(num_uniques[1][j] == 1){ if(!visited[1][j]){ int c = find_color(1, j); q.push_back({1, j, c}); visited[1][j] = true; } } } } else{ for(int j = 0; j < n; ++j){ int color = s[j][index] - 'A'; if(counter[0][j][color] == 1){ --num_uniques[0][j]; } --counter[0][j][color]; if(counter[1][index][color] == 1){ --num_uniques[1][index]; } --counter[1][index][color]; if(num_uniques[0][j] == 1){ if(!visited[0][j]){ int c = find_color(0, j); q.push_back({0, j, c}); visited[0][j] = true; } } } } } cout << q.size() << "\n"; for(int i = q.size()-1; i>=0; --i){ int direction = q[i].direction; int index = q[i].index; int color = q[i].color; char c = color + 'A'; if(direction == 0) cout << "R "; else cout << "K "; cout << index+1 << " " << c << "\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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <vector> using namespace std; const int mx = 2002; const int alfabet = 26; int n, m; string s[mx]; int counter[2][mx][alfabet]; int num_uniques[2][mx]; bool visited[2][mx]; struct turn{ int direction; //row=0, column=1 int index; int color;//A=0, Z=25 }; vector<turn> q; int find_color(int direction, int index){ for(int i = 0; i < alfabet; ++i){ if(counter[direction][index][i] > 0){ return i; } } return 0; } int main(){ ios::sync_with_stdio(false); cin >> n >> m; for(int i = 0; i < n; ++i){ cin >> s[i]; for(int j = 0; j < m; ++j){ int color = s[i][j] - 'A'; if(counter[0][i][color] == 0){ ++num_uniques[0][i]; } ++counter[0][i][color]; if(counter[1][j][color] == 0){ ++num_uniques[1][j]; } ++counter[1][j][color]; } } for(int d = 0; d < 2; ++d){ int sz = (d == 0) ? n : m; for(int i = 0; i < sz; ++i){ if(num_uniques[d][i] == 1){ int c = find_color(d, i); q.push_back({d, i, c}); visited[d][i] = true; } } } for(int i = 0; i < q.size(); ++i){ int direction = q[i].direction; int index = q[i].index; int color = q[i].color; if(direction == 0){ for(int j = 0; j < m; ++j){ int color = s[index][j] - 'A'; if(counter[0][index][color] == 1){ --num_uniques[0][index]; } --counter[0][index][color]; if(counter[1][j][color] == 1){ --num_uniques[1][j]; } --counter[1][j][color]; if(num_uniques[1][j] == 1){ if(!visited[1][j]){ int c = find_color(1, j); q.push_back({1, j, c}); visited[1][j] = true; } } } } else{ for(int j = 0; j < n; ++j){ int color = s[j][index] - 'A'; if(counter[0][j][color] == 1){ --num_uniques[0][j]; } --counter[0][j][color]; if(counter[1][index][color] == 1){ --num_uniques[1][index]; } --counter[1][index][color]; if(num_uniques[0][j] == 1){ if(!visited[0][j]){ int c = find_color(0, j); q.push_back({0, j, c}); visited[0][j] = true; } } } } } cout << q.size() << "\n"; for(int i = q.size()-1; i>=0; --i){ int direction = q[i].direction; int index = q[i].index; int color = q[i].color; char c = color + 'A'; if(direction == 0) cout << "R "; else cout << "K "; cout << index+1 << " " << c << "\n"; } return 0; } |