#include <bits/stdc++.h> using namespace std; int n,m; char arr[2002][2002]; int il[4002][27]; // paski w dol 1->n, paski w prawo n+1->n+m bool inq[4002]; set <char> zn[4002]; queue <int> q; stack <pair <char, pair<int,char>>> st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { cin >> arr[i][j]; zn[i].insert(arr[i][j]); zn[j+n].insert(arr[i][j]); il[i][arr[i][j]-'A']++; il[j+n][arr[i][j]-'A']++; } } for(int i=1; i<=n; i++) { if(zn[i].size() == 1) { inq[i] = 1; q.push(i); } } for(int i=1; i<=m; i++) { if(zn[i+n].size() == 1) { inq[i+n] = 1; q.push(i+n); } } while(!q.empty()) { int v = q.front(); q.pop(); if(v <= n) // jest to R { char c = *zn[v].begin(); for(int i=1; i<=m; i++) { il[n+i][arr[v][i]-'A']--; if(il[n+i][arr[v][i]-'A'] == 0 && !inq[n+i]) // chyba ze jest inq?? (moze if) { zn[n+i].erase(arr[v][i]); if(zn[n+i].size() == 1 && !inq[n+i]) inq[n+i] = 1, q.push(n+i); } } st.push({'R', {v, c}}); } else { char c = *zn[v].begin(); for(int i=1; i<=n; i++) { il[i][arr[i][v-n]-'A']--; if(il[i][arr[i][v-n]-'A'] == 0 && !inq[i]) { zn[i].erase(arr[i][v-n]); if(zn[i].size() == 1 && !inq[i]) inq[i] = 1, q.push(i); } } st.push({'K', {v-n, c}}); } } cout << st.size() << '\n'; while(!st.empty()) { auto x = st.top(); st.pop(); cout << x.first << ' ' << x.second.first << ' ' << x.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 86 87 88 89 | #include <bits/stdc++.h> using namespace std; int n,m; char arr[2002][2002]; int il[4002][27]; // paski w dol 1->n, paski w prawo n+1->n+m bool inq[4002]; set <char> zn[4002]; queue <int> q; stack <pair <char, pair<int,char>>> st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { cin >> arr[i][j]; zn[i].insert(arr[i][j]); zn[j+n].insert(arr[i][j]); il[i][arr[i][j]-'A']++; il[j+n][arr[i][j]-'A']++; } } for(int i=1; i<=n; i++) { if(zn[i].size() == 1) { inq[i] = 1; q.push(i); } } for(int i=1; i<=m; i++) { if(zn[i+n].size() == 1) { inq[i+n] = 1; q.push(i+n); } } while(!q.empty()) { int v = q.front(); q.pop(); if(v <= n) // jest to R { char c = *zn[v].begin(); for(int i=1; i<=m; i++) { il[n+i][arr[v][i]-'A']--; if(il[n+i][arr[v][i]-'A'] == 0 && !inq[n+i]) // chyba ze jest inq?? (moze if) { zn[n+i].erase(arr[v][i]); if(zn[n+i].size() == 1 && !inq[n+i]) inq[n+i] = 1, q.push(n+i); } } st.push({'R', {v, c}}); } else { char c = *zn[v].begin(); for(int i=1; i<=n; i++) { il[i][arr[i][v-n]-'A']--; if(il[i][arr[i][v-n]-'A'] == 0 && !inq[i]) { zn[i].erase(arr[i][v-n]); if(zn[i].size() == 1 && !inq[i]) inq[i] = 1, q.push(i); } } st.push({'K', {v-n, c}}); } } cout << st.size() << '\n'; while(!st.empty()) { auto x = st.top(); st.pop(); cout << x.first << ' ' << x.second.first << ' ' << x.second.second << '\n'; } } |