#include <bits/stdc++.h> #define pb push_back using namespace std; vector <string> p; int n, m; const int maxn = 2e3+1; const int maxm = 2e3+1; int wiersz[maxn][26]; int rzad[maxm][26]; // --- n // |||| m void dbg() { for (int i = 0; i < n; i++) { cout << "wiersz " << i << ": "; for (int j = 0; j < 26; j++) { if (wiersz[i][j] != 0) cout << wiersz[i][j] << ' '; } cout << endl; } for (int i = 0; i < m; i++) { cout << "rzad " << i << ": "; for (int j = 0; j < 26; j++) { if (rzad[i][j] != 0) cout << rzad[i][j] << ' '; } cout << endl; } } void policz() { //liczenie w wierszu for (int i = 0; i < n; i++) { for (auto s : p[i]) wiersz[i][s-'A']++; } //policzyc w rzedzie for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { rzad[i][p[j][i]-'A']++; } } //dbg(); } int ile_wierszy; int ile_rzedow; bool wiersz_uzyty[maxn]; bool rzad_uzyty[maxm]; vector <tuple <char, int, char>> ans; void solve() { ile_wierszy = n; ile_rzedow = m; while (true) { for (int i = 0; i < n; i++) { if (wiersz_uzyty[i]) continue; for (int j = 0; j < 26; j++) { if (wiersz[i][j] == ile_rzedow) { ile_wierszy--; wiersz_uzyty[i] = true; tuple <char, int, char> temp = {'R', i+1, j+'A'}; ans.pb(temp); for (int g = 0; g < m; g++) { rzad[g][j+'A']--; } if (ile_wierszy == 0) return; } } } for (int i = 0; i < m; i++) { if (rzad_uzyty[i]) continue; for (int j = 0; j < 26; j++) { if (rzad[i][j] == ile_wierszy) { ile_rzedow--; rzad_uzyty[i] = true; tuple <char, int, char> temp = {'K', i+1, j+'A'}; ans.pb(temp); for (int g = 0; g < n; g++) { wiersz[g][j+'A']--; } if (ile_rzedow == 0) return; } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { string a; cin >> a; p.pb(a); } policz(); solve(); cout << ans.size() << endl; reverse(ans.begin(), ans.end()); for (auto node : ans) { cout << get<0>(node) << ' ' << get<1>(node) << ' ' << get<2>(node) << endl; } }
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 | #include <bits/stdc++.h> #define pb push_back using namespace std; vector <string> p; int n, m; const int maxn = 2e3+1; const int maxm = 2e3+1; int wiersz[maxn][26]; int rzad[maxm][26]; // --- n // |||| m void dbg() { for (int i = 0; i < n; i++) { cout << "wiersz " << i << ": "; for (int j = 0; j < 26; j++) { if (wiersz[i][j] != 0) cout << wiersz[i][j] << ' '; } cout << endl; } for (int i = 0; i < m; i++) { cout << "rzad " << i << ": "; for (int j = 0; j < 26; j++) { if (rzad[i][j] != 0) cout << rzad[i][j] << ' '; } cout << endl; } } void policz() { //liczenie w wierszu for (int i = 0; i < n; i++) { for (auto s : p[i]) wiersz[i][s-'A']++; } //policzyc w rzedzie for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { rzad[i][p[j][i]-'A']++; } } //dbg(); } int ile_wierszy; int ile_rzedow; bool wiersz_uzyty[maxn]; bool rzad_uzyty[maxm]; vector <tuple <char, int, char>> ans; void solve() { ile_wierszy = n; ile_rzedow = m; while (true) { for (int i = 0; i < n; i++) { if (wiersz_uzyty[i]) continue; for (int j = 0; j < 26; j++) { if (wiersz[i][j] == ile_rzedow) { ile_wierszy--; wiersz_uzyty[i] = true; tuple <char, int, char> temp = {'R', i+1, j+'A'}; ans.pb(temp); for (int g = 0; g < m; g++) { rzad[g][j+'A']--; } if (ile_wierszy == 0) return; } } } for (int i = 0; i < m; i++) { if (rzad_uzyty[i]) continue; for (int j = 0; j < 26; j++) { if (rzad[i][j] == ile_wierszy) { ile_rzedow--; rzad_uzyty[i] = true; tuple <char, int, char> temp = {'K', i+1, j+'A'}; ans.pb(temp); for (int g = 0; g < n; g++) { wiersz[g][j+'A']--; } if (ile_rzedow == 0) return; } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { string a; cin >> a; p.pb(a); } policz(); solve(); cout << ans.size() << endl; reverse(ans.begin(), ans.end()); for (auto node : ans) { cout << get<0>(node) << ' ' << get<1>(node) << ' ' << get<2>(node) << endl; } } |