#include<bits/stdc++.h> using namespace std; int n,m; string board[2010]; int rows[2010][30]; int cols[2010][30]; set<int> ava_row,ava_col; vector<pair<int,char>> ans; queue<pair<int,char>> que; void del_row(int idx, int letter){ ava_row.erase(idx); for(int j : ava_col){ cols[j][letter + 1]--; if(cols[j][letter + 1] == 0){ cols[j][0]--; if(cols[j][0] == 1){ // for(int i = 1; i <= 'Z' - 'A' + 1; i++){ // if(cols[j][i] > 0){ // que.push({j,i - 1 + 'a'}); // break; // } // } que.push({j,board[(*ava_row.begin())][j] - 'A' + 'a'}); } } } } void del_col(int idx, int letter){ ava_col.erase(idx); for(int i : ava_row){ rows[i][letter + 1]--; if(rows[i][letter + 1] == 0){ rows[i][0]--; if(rows[i][0] == 1){ // for(int j = 1; j <= 'Z' - 'A' + 1; j++){ // if(rows[i][j] > 0){ // que.push({i,j - 1 + 'A'}); // break; // } // } que.push({i,board[i][(*ava_col.begin())]}); } } } } int main(){ cin >> n >> m; for(int i = 0; i < n; i++){ cin >> board[i]; } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ rows[i][board[i][j] - 'A' + 1]++; if(rows[i][board[i][j] - 'A' + 1] == 1){ rows[i][0]++; } cols[j][board[i][j] - 'A' + 1]++; if(cols[j][board[i][j] - 'A' + 1] == 1){ cols[j][0]++; } } } for(int i = 0; i < n; i++){ ava_row.insert(i); if(rows[i][0] == 1){ que.push({i,board[i][0]}); } } for(int i = 0; i < m; i++){ ava_col.insert(i); if(cols[i][0] == 1){ que.push({i,board[0][i] + 'a' - 'A'}); } } pair<int,char> tmpp; while(!que.empty()){ tmpp = que.front(); que.pop(); ans.push_back(tmpp); if(tmpp.second < 'a'){ del_row(tmpp.first, tmpp.second - 'A'); } else { del_col(tmpp.first, tmpp.second - 'a'); } } reverse(ans.begin(),ans.end()); cout << ans.size() << "\n"; for(auto i : ans){ if(i.second > 'Z'){ cout << "K "; i.second += 'A' - 'a'; } else { cout << "R "; } cout << i.first + 1 << " " << i.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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include<bits/stdc++.h> using namespace std; int n,m; string board[2010]; int rows[2010][30]; int cols[2010][30]; set<int> ava_row,ava_col; vector<pair<int,char>> ans; queue<pair<int,char>> que; void del_row(int idx, int letter){ ava_row.erase(idx); for(int j : ava_col){ cols[j][letter + 1]--; if(cols[j][letter + 1] == 0){ cols[j][0]--; if(cols[j][0] == 1){ // for(int i = 1; i <= 'Z' - 'A' + 1; i++){ // if(cols[j][i] > 0){ // que.push({j,i - 1 + 'a'}); // break; // } // } que.push({j,board[(*ava_row.begin())][j] - 'A' + 'a'}); } } } } void del_col(int idx, int letter){ ava_col.erase(idx); for(int i : ava_row){ rows[i][letter + 1]--; if(rows[i][letter + 1] == 0){ rows[i][0]--; if(rows[i][0] == 1){ // for(int j = 1; j <= 'Z' - 'A' + 1; j++){ // if(rows[i][j] > 0){ // que.push({i,j - 1 + 'A'}); // break; // } // } que.push({i,board[i][(*ava_col.begin())]}); } } } } int main(){ cin >> n >> m; for(int i = 0; i < n; i++){ cin >> board[i]; } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ rows[i][board[i][j] - 'A' + 1]++; if(rows[i][board[i][j] - 'A' + 1] == 1){ rows[i][0]++; } cols[j][board[i][j] - 'A' + 1]++; if(cols[j][board[i][j] - 'A' + 1] == 1){ cols[j][0]++; } } } for(int i = 0; i < n; i++){ ava_row.insert(i); if(rows[i][0] == 1){ que.push({i,board[i][0]}); } } for(int i = 0; i < m; i++){ ava_col.insert(i); if(cols[i][0] == 1){ que.push({i,board[0][i] + 'a' - 'A'}); } } pair<int,char> tmpp; while(!que.empty()){ tmpp = que.front(); que.pop(); ans.push_back(tmpp); if(tmpp.second < 'a'){ del_row(tmpp.first, tmpp.second - 'A'); } else { del_col(tmpp.first, tmpp.second - 'a'); } } reverse(ans.begin(),ans.end()); cout << ans.size() << "\n"; for(auto i : ans){ if(i.second > 'Z'){ cout << "K "; i.second += 'A' - 'a'; } else { cout << "R "; } cout << i.first + 1 << " " << i.second << "\n"; } } |