#include <iostream> #include <queue> #include <string> #include <vector> #define MAXN 2002 #define ALFA 26 using namespace std; int n, m; string s[MAXN]; int rs[MAXN][ALFA + 2], cs[MAXN][ALFA + 2]; vector<pair<string, int>> res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = s[i][j] - 'A'; rs[i][k]++; cs[j][k]++; } } int leftn = n, leftm = m; while (leftn > 0 && leftm > 0) { for (int k = 0; k < ALFA; k++) { if (leftm) { for (int i = 0; i < n; i++) { if (leftn && rs[i][k] == leftm && !rs[i][ALFA]) { rs[i][ALFA] = 1; leftn--; //cerr << "r " << i << " " << (char)(k + 'A') << leftn << "\n"; string b = "R"; b.push_back('A' + k); res.push_back({b, i + 1}); //for (int p = 0; p < m; p++) cerr << cs[p][k] << " "; cerr << "\n"; for (int j = 0; j < m; j++) { cs[j][k]--; } //for (int p = 0; p < m; p++) cerr << cs[p][k] << " "; cerr << "\n"; } } } if (leftn) { for (int j = 0; j < m; j++) { if (leftm && cs[j][k] == leftn && !cs[j][ALFA]) { cs[j][ALFA] = 1; leftm--; //cerr << "c " << j << " " << (char)(k + 'A') << leftm << "\n"; string b = "K"; b.push_back('A' + k); res.push_back({b, j + 1}); //for (int p = 0; p < n; p++) cerr << rs[p][k] << " "; cerr << "\n"; for (int i = 0; i < n; i++) { rs[i][k]--; } //for (int p = 0; p < n; p++) cerr << rs[p][k] << " "; cerr << "\n"; } } } } } cout << res.size() << "\n"; for (auto it = res.rbegin(); it != res.rend(); it++) { cout << it->first[0] << " " << it->second << " " << it->first[1] << "\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 | #include <iostream> #include <queue> #include <string> #include <vector> #define MAXN 2002 #define ALFA 26 using namespace std; int n, m; string s[MAXN]; int rs[MAXN][ALFA + 2], cs[MAXN][ALFA + 2]; vector<pair<string, int>> res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = s[i][j] - 'A'; rs[i][k]++; cs[j][k]++; } } int leftn = n, leftm = m; while (leftn > 0 && leftm > 0) { for (int k = 0; k < ALFA; k++) { if (leftm) { for (int i = 0; i < n; i++) { if (leftn && rs[i][k] == leftm && !rs[i][ALFA]) { rs[i][ALFA] = 1; leftn--; //cerr << "r " << i << " " << (char)(k + 'A') << leftn << "\n"; string b = "R"; b.push_back('A' + k); res.push_back({b, i + 1}); //for (int p = 0; p < m; p++) cerr << cs[p][k] << " "; cerr << "\n"; for (int j = 0; j < m; j++) { cs[j][k]--; } //for (int p = 0; p < m; p++) cerr << cs[p][k] << " "; cerr << "\n"; } } } if (leftn) { for (int j = 0; j < m; j++) { if (leftm && cs[j][k] == leftn && !cs[j][ALFA]) { cs[j][ALFA] = 1; leftm--; //cerr << "c " << j << " " << (char)(k + 'A') << leftm << "\n"; string b = "K"; b.push_back('A' + k); res.push_back({b, j + 1}); //for (int p = 0; p < n; p++) cerr << rs[p][k] << " "; cerr << "\n"; for (int i = 0; i < n; i++) { rs[i][k]--; } //for (int p = 0; p < n; p++) cerr << rs[p][k] << " "; cerr << "\n"; } } } } } cout << res.size() << "\n"; for (auto it = res.rbegin(); it != res.rend(); it++) { cout << it->first[0] << " " << it->second << " " << it->first[1] << "\n"; } return 0; } |