#include <bits/stdc++.h> using namespace std; pair<int, int> max_e(vector<int>& a){ int m = 0, index = 0; for(int i=0; i<26; i++) if(a[i] > m){ m = a[i]; index = i; } return {m, index}; } int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n, m; cin>>n>>m; vector<bool> deletedrow(n, 0), deletedcolumn(m, 0); vector<vector<int>> rows(n, vector<int>(27, 0)); vector<vector<int>> columns(m, vector<int>(27, 0)); //---------------------------- for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ char x; cin>>x; int id = (int)x - (int)('A'); rows[i][id]++; rows[i][26]++; columns[j][id]++; columns[j][26]++; } } //---------------------------- vector<pair<bool, pair<int, int>>> answer; bool notyet = 1; while(notyet){ notyet = 0; for(int i=0; i<n; i++){ if(rows[i][26] <= 0) continue; pair<int, int> temp = max_e(rows[i]); if(rows[i][26] > 0) notyet = 1; if(rows[i][26] == temp.first){ answer.push_back({0, {i+1, temp.second}}); rows[i][26] = 0; rows[i][temp.second] = 0; deletedrow[i] = 1; for(int j=0; j<m; j++){ if(deletedcolumn[j]) continue; columns[j][temp.second]--; columns[j][26]--; } } } for(int i=0; i<m; i++){ if(columns[i][26] <= 0) continue; pair<int, int> temp = max_e(columns[i]); if(columns[i][26] > 0) notyet = 1; if(columns[i][26] == temp.first){ answer.push_back({1, {i+1, temp.second}}); columns[i][26] = 0; columns[i][temp.second] = 0; deletedcolumn[i] = 1; for(int j=0; j<n; j++){ if(deletedrow[j]) continue; rows[j][temp.second]--; rows[j][26]--; } } } } //---------------------------- cout<<answer.size()<<'\n'; for(int i=answer.size()-1; i>=0; i--){ if(answer[i].first) cout<<"K "; else cout<<"R "; cout<<answer[i].second.first<<" "<<(char)(answer[i].second.second + (int)('A'))<<'\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 | #include <bits/stdc++.h> using namespace std; pair<int, int> max_e(vector<int>& a){ int m = 0, index = 0; for(int i=0; i<26; i++) if(a[i] > m){ m = a[i]; index = i; } return {m, index}; } int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n, m; cin>>n>>m; vector<bool> deletedrow(n, 0), deletedcolumn(m, 0); vector<vector<int>> rows(n, vector<int>(27, 0)); vector<vector<int>> columns(m, vector<int>(27, 0)); //---------------------------- for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ char x; cin>>x; int id = (int)x - (int)('A'); rows[i][id]++; rows[i][26]++; columns[j][id]++; columns[j][26]++; } } //---------------------------- vector<pair<bool, pair<int, int>>> answer; bool notyet = 1; while(notyet){ notyet = 0; for(int i=0; i<n; i++){ if(rows[i][26] <= 0) continue; pair<int, int> temp = max_e(rows[i]); if(rows[i][26] > 0) notyet = 1; if(rows[i][26] == temp.first){ answer.push_back({0, {i+1, temp.second}}); rows[i][26] = 0; rows[i][temp.second] = 0; deletedrow[i] = 1; for(int j=0; j<m; j++){ if(deletedcolumn[j]) continue; columns[j][temp.second]--; columns[j][26]--; } } } for(int i=0; i<m; i++){ if(columns[i][26] <= 0) continue; pair<int, int> temp = max_e(columns[i]); if(columns[i][26] > 0) notyet = 1; if(columns[i][26] == temp.first){ answer.push_back({1, {i+1, temp.second}}); columns[i][26] = 0; columns[i][temp.second] = 0; deletedcolumn[i] = 1; for(int j=0; j<n; j++){ if(deletedrow[j]) continue; rows[j][temp.second]--; rows[j][26]--; } } } } //---------------------------- cout<<answer.size()<<'\n'; for(int i=answer.size()-1; i>=0; i--){ if(answer[i].first) cout<<"K "; else cout<<"R "; cout<<answer[i].second.first<<" "<<(char)(answer[i].second.second + (int)('A'))<<'\n'; } return 0; } |