#include <bits/stdc++.h> using namespace std; typedef long long ll; int ile[2001][2][257]; int ile2[2001][2]; bool vis[2001][2]; bool usu[2001][2001]; void usun_pole(int a, int b, char c){ ile[a][0][(int)c]--; if (ile[a][0][(int)c] == 0) ile2[a][0]--; ile[b][1][(int)c]--; if (ile[b][1][(int)c] == 0) ile2[b][1]--; } signed main(){ //ios_base::sync_with_stdio(0); //cin.tie(0); int i; int n,m; cin>>n>>m; string tab[n]; for (i = 0; i < n; i++){ cin>>tab[i]; for (int j = 0; j < m; j++){ ile[i][0][(int)tab[i][j]]++; if (ile[i][0][(int)tab[i][j]] == 1) ile2[i][0]++; ile[j][1][(int)tab[i][j]]++; if (ile[j][1][(int)tab[i][j]] == 1) ile2[j][1]++; } } /* for (i = 0; i < n; i++) cout<<ile2[i][0]<<" "; cout<<"\n"; for (i = 0; i < m; i++) cout<<ile2[i][1]<<" "; cout<<"\n";*/ vector<pair<char,pair<int,char> > > wynik; int wy1 = n; int wy2 = m; while (wy1 && wy2){ bool good = 0; for (i = 0; i < n; i++){ if (!vis[i][0] && ile2[i][0] == 1){ wy1--; vis[i][0] = 1; char kolor; for (int j = 0; j < m; j++){ if (!usu[i][j]){ kolor = tab[i][j]; usu[i][j] = 1; usun_pole(i,j,tab[i][j]); } } //cout<<"R"<<i+1<<" "<<kolor<<"\n"; //exit(0); wynik.push_back({'R',{i,kolor}}); good = 1; break; } } if (good) continue; for (int j = 0; j < m; j++){ if (!vis[j][1] && ile2[j][1] == 1){ wy2--; vis[j][1] = 1; char kolor; for (i = 0; i < n; i++){ if (!usu[i][j]){ kolor = tab[i][j]; usu[i][j] = 1; usun_pole(i,j,tab[i][j]); } } //cout<<"K"<<j+1<<" "<<kolor<<"\n"; //exit(0); wynik.push_back({'K',{j,kolor}}); break; } } } cout<<wynik.size()<<"\n"; reverse(wynik.begin(),wynik.end()); for (auto j : wynik){ cout<<j.first<<" "<<j.second.first+1<<" "<<j.second.second<<"\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 85 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int ile[2001][2][257]; int ile2[2001][2]; bool vis[2001][2]; bool usu[2001][2001]; void usun_pole(int a, int b, char c){ ile[a][0][(int)c]--; if (ile[a][0][(int)c] == 0) ile2[a][0]--; ile[b][1][(int)c]--; if (ile[b][1][(int)c] == 0) ile2[b][1]--; } signed main(){ //ios_base::sync_with_stdio(0); //cin.tie(0); int i; int n,m; cin>>n>>m; string tab[n]; for (i = 0; i < n; i++){ cin>>tab[i]; for (int j = 0; j < m; j++){ ile[i][0][(int)tab[i][j]]++; if (ile[i][0][(int)tab[i][j]] == 1) ile2[i][0]++; ile[j][1][(int)tab[i][j]]++; if (ile[j][1][(int)tab[i][j]] == 1) ile2[j][1]++; } } /* for (i = 0; i < n; i++) cout<<ile2[i][0]<<" "; cout<<"\n"; for (i = 0; i < m; i++) cout<<ile2[i][1]<<" "; cout<<"\n";*/ vector<pair<char,pair<int,char> > > wynik; int wy1 = n; int wy2 = m; while (wy1 && wy2){ bool good = 0; for (i = 0; i < n; i++){ if (!vis[i][0] && ile2[i][0] == 1){ wy1--; vis[i][0] = 1; char kolor; for (int j = 0; j < m; j++){ if (!usu[i][j]){ kolor = tab[i][j]; usu[i][j] = 1; usun_pole(i,j,tab[i][j]); } } //cout<<"R"<<i+1<<" "<<kolor<<"\n"; //exit(0); wynik.push_back({'R',{i,kolor}}); good = 1; break; } } if (good) continue; for (int j = 0; j < m; j++){ if (!vis[j][1] && ile2[j][1] == 1){ wy2--; vis[j][1] = 1; char kolor; for (i = 0; i < n; i++){ if (!usu[i][j]){ kolor = tab[i][j]; usu[i][j] = 1; usun_pole(i,j,tab[i][j]); } } //cout<<"K"<<j+1<<" "<<kolor<<"\n"; //exit(0); wynik.push_back({'K',{j,kolor}}); break; } } } cout<<wynik.size()<<"\n"; reverse(wynik.begin(),wynik.end()); for (auto j : wynik){ cout<<j.first<<" "<<j.second.first+1<<" "<<j.second.second<<"\n"; } } |