#include <bits/stdc++.h>
using namespace std;
struct st{
char rk;
int x;
int col;
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
vector <int> wiersz(n+1);
vector <int> kolumna(m+1);
vector <vector<int>> wiersze(n+1, vector<int>(26));
vector <vector<int>> kolumny(m+1, vector<int>(26));
for(int i = 1; i<=n; ++i){
for(int j = 1; j<=m; ++j){
char c;
cin >> c;
if(wiersze[i][c-'A'] == 0){
wiersz[i]++;
}
wiersze[i][c-'A']++;
if(kolumny[j][c-'A'] == 0){
kolumna[j]++;
}
kolumny[j][c-'A']++;
}
}
stack <st> s;
while(true){
//cerr << s.size() << "\n";
bool found = false;
for(int i = 1; i<=n; ++i){
if(wiersz[i] == 1){
found = true;
wiersz[i] = 0;
int col;
for(int j = 0; j<26; ++j){
if(wiersze[i][j]) col = j;
}
s.push({'R', i, col});
//cerr << "R " << i << "\n";
for(int j = 1; j<=m; ++j){
kolumny[j][col]--;
if(kolumny[j][col] == 0) kolumna[j]--;
}
}
}
for(int j = 1; j<=m; ++j){
if(kolumna[j] == 1){
found = true;
kolumna[j] = 0;
int col;
for(int i = 0; i<26; ++i){
if(kolumny[j][i]) col = i;
}
s.push({'K', j, col});
//cerr << "K " << j << "\n";
for(int i = 1; i<=n; ++i){
wiersze[i][col]--;
if(wiersze[i][col] == 0) wiersz[i]--;
}
}
}
if(!found) break;
}
cout << s.size() << "\n";
while(!s.empty()){
st act = s.top();
s.pop();
cout << act.rk << " " << act.x << " " << (char)(act.col+'A') << "\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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <bits/stdc++.h> using namespace std; struct st{ char rk; int x; int col; }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector <int> wiersz(n+1); vector <int> kolumna(m+1); vector <vector<int>> wiersze(n+1, vector<int>(26)); vector <vector<int>> kolumny(m+1, vector<int>(26)); for(int i = 1; i<=n; ++i){ for(int j = 1; j<=m; ++j){ char c; cin >> c; if(wiersze[i][c-'A'] == 0){ wiersz[i]++; } wiersze[i][c-'A']++; if(kolumny[j][c-'A'] == 0){ kolumna[j]++; } kolumny[j][c-'A']++; } } stack <st> s; while(true){ //cerr << s.size() << "\n"; bool found = false; for(int i = 1; i<=n; ++i){ if(wiersz[i] == 1){ found = true; wiersz[i] = 0; int col; for(int j = 0; j<26; ++j){ if(wiersze[i][j]) col = j; } s.push({'R', i, col}); //cerr << "R " << i << "\n"; for(int j = 1; j<=m; ++j){ kolumny[j][col]--; if(kolumny[j][col] == 0) kolumna[j]--; } } } for(int j = 1; j<=m; ++j){ if(kolumna[j] == 1){ found = true; kolumna[j] = 0; int col; for(int i = 0; i<26; ++i){ if(kolumny[j][i]) col = i; } s.push({'K', j, col}); //cerr << "K " << j << "\n"; for(int i = 1; i<=n; ++i){ wiersze[i][col]--; if(wiersze[i][col] == 0) wiersz[i]--; } } } if(!found) break; } cout << s.size() << "\n"; while(!s.empty()){ st act = s.top(); s.pop(); cout << act.rk << " " << act.x << " " << (char)(act.col+'A') << "\n"; } } |
English