#include <bits/stdc++.h> using namespace std; int LiczbaN[2001][27]; int LiczbaM[2001][27]; int MaksN[2001],MaksM[2001]; int Tab[2001][2001]; struct trips{ char typ; int rzad; int lit; }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; for (int i = 1; i <= n;i++){ for (int j = 1; j <= m;j++){ char c; cin >> c; int d = (int)c - 'A' + 1; Tab[i][j] = d; LiczbaN[i][d]++; LiczbaM[j][d]++; MaksN[i] = max(MaksN[i],LiczbaN[i][d]); MaksM[j] = max(MaksM[j],LiczbaM[j][d]); } } int liczba_en = n, liczba_em = m; vector <trips> V; while(liczba_en + liczba_em > 0){ for (int i = 1;i <= n;i++){ if(MaksN[i] >= liczba_em){ liczba_en--; int Litera = -100; for (int j = 1; j <= 26;j++){ if(LiczbaN[i][j] == MaksN[i]){ Litera = j; } } V.push_back({'R',i,Litera}); for (int j = 1; j <= m;j++){ LiczbaM[j][Tab[i][j]]--; MaksM[j] = -1; for (int k = 1; k <= 26;k++){ MaksM[j] = max(MaksM[j],LiczbaM[j][k]); } } for (int k = 1; k <= 26;k++){ LiczbaN[i][k] = -1; } MaksN[i] = -1; } } for (int i = 1;i <= m;i++){ if(MaksM[i] >= liczba_en){ liczba_em--; int Litera = -100; for (int j = 1; j <= 26;j++){ if(LiczbaM[i][j] == MaksM[i]){ Litera = j; } } V.push_back({'K',i,Litera}); for (int j = 1; j <= n;j++){ LiczbaN[j][Tab[j][i]]--; MaksN[j] = -1; for (int k = 1; k <= 26;k++){ MaksN[j] = max(MaksN[j],LiczbaN[j][k]); } } for (int k = 1; k <= 26;k++){ LiczbaM[i][k] = -1; } MaksM[i] = -1; } } } cout << V.size() << '\n'; for (int i = V.size() - 1; i >= 0;i--){ cout << V[i].typ << " " << V[i].rzad << " " <<char(V[i].lit + (int)'A' - 1)<< '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; int LiczbaN[2001][27]; int LiczbaM[2001][27]; int MaksN[2001],MaksM[2001]; int Tab[2001][2001]; struct trips{ char typ; int rzad; int lit; }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; for (int i = 1; i <= n;i++){ for (int j = 1; j <= m;j++){ char c; cin >> c; int d = (int)c - 'A' + 1; Tab[i][j] = d; LiczbaN[i][d]++; LiczbaM[j][d]++; MaksN[i] = max(MaksN[i],LiczbaN[i][d]); MaksM[j] = max(MaksM[j],LiczbaM[j][d]); } } int liczba_en = n, liczba_em = m; vector <trips> V; while(liczba_en + liczba_em > 0){ for (int i = 1;i <= n;i++){ if(MaksN[i] >= liczba_em){ liczba_en--; int Litera = -100; for (int j = 1; j <= 26;j++){ if(LiczbaN[i][j] == MaksN[i]){ Litera = j; } } V.push_back({'R',i,Litera}); for (int j = 1; j <= m;j++){ LiczbaM[j][Tab[i][j]]--; MaksM[j] = -1; for (int k = 1; k <= 26;k++){ MaksM[j] = max(MaksM[j],LiczbaM[j][k]); } } for (int k = 1; k <= 26;k++){ LiczbaN[i][k] = -1; } MaksN[i] = -1; } } for (int i = 1;i <= m;i++){ if(MaksM[i] >= liczba_en){ liczba_em--; int Litera = -100; for (int j = 1; j <= 26;j++){ if(LiczbaM[i][j] == MaksM[i]){ Litera = j; } } V.push_back({'K',i,Litera}); for (int j = 1; j <= n;j++){ LiczbaN[j][Tab[j][i]]--; MaksN[j] = -1; for (int k = 1; k <= 26;k++){ MaksN[j] = max(MaksN[j],LiczbaN[j][k]); } } for (int k = 1; k <= 26;k++){ LiczbaM[i][k] = -1; } MaksM[i] = -1; } } } cout << V.size() << '\n'; for (int i = V.size() - 1; i >= 0;i--){ cout << V[i].typ << " " << V[i].rzad << " " <<char(V[i].lit + (int)'A' - 1)<< '\n'; } } |