#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif // #define MULTI_TASK true const int INF = 1000 * 1000 * 1000; const LL INFLL = 1e18; const int MAKSN = 2000 + 13; const int ALP_SIZE = 30; const LL MOD = 123456789LL; LL N, M, k, l, a, b, c, d; string s; vector<string> v; int rows_cnt[MAKSN][ALP_SIZE], cols_cnt[MAKSN][ALP_SIZE]; int rows_sub[ALP_SIZE], cols_sub[ALP_SIZE]; bool row_deleted[MAKSN], cols_deleted[MAKSN]; vector<tuple<char, int, char> > answers; void zeruj() { v.clear(); } int ord(char c) { return c - 'A'; } void readIn() { cin >> N >> M; REP(i, N) { cin >> s; v.PB(s); } REP(i, N) { REP(j, M) { rows_cnt[i][ord(v[i][j])] += 1; cols_cnt[j][ord(v[i][j])] += 1; } } } bool is_single_color(int cnts[], int deduct[]) { int colors_cnt = 0; for (int i = 0; i < ALP_SIZE; i++) { if (cnts[i] - deduct[i] != 0) colors_cnt += 1; } return colors_cnt == 1; } int find_color(int cnts[], int deduct[]) { for (int i = 0; i < ALP_SIZE; i++) { if (cnts[i] - deduct[i] != 0) return i; } return -1; } void printState() { for(int i = 0; i < N; i++) { cerr << "R " << i << ": "; for (int j = 0; j < ALP_SIZE; j++) { if (rows_cnt[i][j]) cerr << char(j + 'A') << rows_cnt[i][j] - cols_sub[j] << ", "; } if(row_deleted[i]) cerr << "Deleted "; cerr << "\n"; } cerr << "\n\n"; for(int i = 0; i < M; i++) { cerr << "C " << i << ": "; for (int j = 0; j < ALP_SIZE; j++) { if (cols_cnt[i][j]) cerr << char(j + 'A') << cols_cnt[i][j] - rows_sub[j] << ", "; } if(cols_deleted[i]) cerr << "Deleted "; cerr << "\n"; } cerr << "\n\n"; } void solve() { while (true) { char typ; int nr = -1, color; for (int i = 0; i < N; i++) { if (row_deleted[i]) continue; if (is_single_color(rows_cnt[i], cols_sub)) { typ = 'R'; color = find_color(rows_cnt[i], cols_sub); nr = i; row_deleted[i] = true; rows_sub[color] += 1; break; } } if (nr == -1) { for (int i = 0; i < M; i++) { if (cols_deleted[i]) continue; if (is_single_color(cols_cnt[i], rows_sub)) { typ = 'K'; color = find_color(cols_cnt[i], rows_sub); nr = i; cols_deleted[i] = true; cols_sub[color] += 1; break; } } } if (nr == -1) break; answers.emplace_back(typ, nr, char(color + 'A')); } reverse(answers.begin(), answers.end()); cout << answers.size() << "\n"; for (auto t : answers) { cout << get<0>(t) << " " << get<1>(t) + 1 << " " << get<2>(t) << "\n"; } } int main() { ios_base::sync_with_stdio(0); #ifdef MULTI_TASK int test; cin >> test; while (test--) { #endif zeruj(); readIn(); solve(); #ifdef MULTI_TASK } #endif return 0; } // AAPAA // APPAA // AAPAA // AAPAA // APPPA
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif // #define MULTI_TASK true const int INF = 1000 * 1000 * 1000; const LL INFLL = 1e18; const int MAKSN = 2000 + 13; const int ALP_SIZE = 30; const LL MOD = 123456789LL; LL N, M, k, l, a, b, c, d; string s; vector<string> v; int rows_cnt[MAKSN][ALP_SIZE], cols_cnt[MAKSN][ALP_SIZE]; int rows_sub[ALP_SIZE], cols_sub[ALP_SIZE]; bool row_deleted[MAKSN], cols_deleted[MAKSN]; vector<tuple<char, int, char> > answers; void zeruj() { v.clear(); } int ord(char c) { return c - 'A'; } void readIn() { cin >> N >> M; REP(i, N) { cin >> s; v.PB(s); } REP(i, N) { REP(j, M) { rows_cnt[i][ord(v[i][j])] += 1; cols_cnt[j][ord(v[i][j])] += 1; } } } bool is_single_color(int cnts[], int deduct[]) { int colors_cnt = 0; for (int i = 0; i < ALP_SIZE; i++) { if (cnts[i] - deduct[i] != 0) colors_cnt += 1; } return colors_cnt == 1; } int find_color(int cnts[], int deduct[]) { for (int i = 0; i < ALP_SIZE; i++) { if (cnts[i] - deduct[i] != 0) return i; } return -1; } void printState() { for(int i = 0; i < N; i++) { cerr << "R " << i << ": "; for (int j = 0; j < ALP_SIZE; j++) { if (rows_cnt[i][j]) cerr << char(j + 'A') << rows_cnt[i][j] - cols_sub[j] << ", "; } if(row_deleted[i]) cerr << "Deleted "; cerr << "\n"; } cerr << "\n\n"; for(int i = 0; i < M; i++) { cerr << "C " << i << ": "; for (int j = 0; j < ALP_SIZE; j++) { if (cols_cnt[i][j]) cerr << char(j + 'A') << cols_cnt[i][j] - rows_sub[j] << ", "; } if(cols_deleted[i]) cerr << "Deleted "; cerr << "\n"; } cerr << "\n\n"; } void solve() { while (true) { char typ; int nr = -1, color; for (int i = 0; i < N; i++) { if (row_deleted[i]) continue; if (is_single_color(rows_cnt[i], cols_sub)) { typ = 'R'; color = find_color(rows_cnt[i], cols_sub); nr = i; row_deleted[i] = true; rows_sub[color] += 1; break; } } if (nr == -1) { for (int i = 0; i < M; i++) { if (cols_deleted[i]) continue; if (is_single_color(cols_cnt[i], rows_sub)) { typ = 'K'; color = find_color(cols_cnt[i], rows_sub); nr = i; cols_deleted[i] = true; cols_sub[color] += 1; break; } } } if (nr == -1) break; answers.emplace_back(typ, nr, char(color + 'A')); } reverse(answers.begin(), answers.end()); cout << answers.size() << "\n"; for (auto t : answers) { cout << get<0>(t) << " " << get<1>(t) + 1 << " " << get<2>(t) << "\n"; } } int main() { ios_base::sync_with_stdio(0); #ifdef MULTI_TASK int test; cin >> test; while (test--) { #endif zeruj(); readIn(); solve(); #ifdef MULTI_TASK } #endif return 0; } // AAPAA // APPAA // AAPAA // AAPAA // APPPA |