#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 2000 + 7; int difr[N], difc[N], ilc[N][30], ilr[N][30]; string tab[N]; vector<pair<char, pair<int, char>>> answer; void Print() { cout << answer.size() << "\n"; reverse(answer.begin(), answer.end()); for(int i = 0; i < (int)answer.size(); ++i) cout << answer[i].st << " " << answer[i].nd.st << " " << answer[i].nd.nd << "\n"; } void Solve() { int n, m; queue<pair<char, pair<int, char>>> q; cin >> n >> m; for(int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] = '#' + tab[i]; for(int j = 1; j <= m; ++j) { ++ilc[j][tab[i][j] - 'A']; ++ilr[i][tab[i][j] - 'A']; if(ilc[j][tab[i][j] - 'A'] == 1) ++difc[j]; if(ilr[i][tab[i][j] - 'A'] == 1) ++difr[i]; } if(difr[i] == 1) q.push(make_pair('R', make_pair(i, tab[i][1]))); } for(int i = 1; i <= m; ++i) if(difc[i] == 1) q.push(make_pair('K', make_pair(i, tab[1][i]))); while(q.size() > 0) { pair<char, pair<int, char>> cur; cur = q.front(); q.pop(); answer.pb(cur); if(cur.st == 'R') { int i = cur.nd.st; for(int j = 1; j <= m; ++j) { --ilc[j][tab[i][j] - 'A']; if(ilc[j][tab[i][j] - 'A'] == 0) { --difc[j]; if(difc[j] == 1) { for(int k = 0; k < 26; ++k) if(ilc[j][k] > 0) { q.push(make_pair('K', make_pair(j, char(k + 'A')))); break; } } } } continue; } int j = cur.nd.st; for(int i = 1; i <= n; ++i) { --ilr[i][tab[i][j] - 'A']; if(ilr[i][tab[i][j] - 'A'] == 0) { --difr[i]; if(difr[i] == 1) { for(int k = 0; k < 26; ++k) if(ilr[i][k] > 0) { q.push(make_pair('R', make_pair(i, char(k + 'A')))); break; } } } } } Print(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); 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 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 106 107 108 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 2000 + 7; int difr[N], difc[N], ilc[N][30], ilr[N][30]; string tab[N]; vector<pair<char, pair<int, char>>> answer; void Print() { cout << answer.size() << "\n"; reverse(answer.begin(), answer.end()); for(int i = 0; i < (int)answer.size(); ++i) cout << answer[i].st << " " << answer[i].nd.st << " " << answer[i].nd.nd << "\n"; } void Solve() { int n, m; queue<pair<char, pair<int, char>>> q; cin >> n >> m; for(int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] = '#' + tab[i]; for(int j = 1; j <= m; ++j) { ++ilc[j][tab[i][j] - 'A']; ++ilr[i][tab[i][j] - 'A']; if(ilc[j][tab[i][j] - 'A'] == 1) ++difc[j]; if(ilr[i][tab[i][j] - 'A'] == 1) ++difr[i]; } if(difr[i] == 1) q.push(make_pair('R', make_pair(i, tab[i][1]))); } for(int i = 1; i <= m; ++i) if(difc[i] == 1) q.push(make_pair('K', make_pair(i, tab[1][i]))); while(q.size() > 0) { pair<char, pair<int, char>> cur; cur = q.front(); q.pop(); answer.pb(cur); if(cur.st == 'R') { int i = cur.nd.st; for(int j = 1; j <= m; ++j) { --ilc[j][tab[i][j] - 'A']; if(ilc[j][tab[i][j] - 'A'] == 0) { --difc[j]; if(difc[j] == 1) { for(int k = 0; k < 26; ++k) if(ilc[j][k] > 0) { q.push(make_pair('K', make_pair(j, char(k + 'A')))); break; } } } } continue; } int j = cur.nd.st; for(int i = 1; i <= n; ++i) { --ilr[i][tab[i][j] - 'A']; if(ilr[i][tab[i][j] - 'A'] == 0) { --difr[i]; if(difr[i] == 1) { for(int k = 0; k < 26; ++k) if(ilr[i][k] > 0) { q.push(make_pair('R', make_pair(i, char(k + 'A')))); break; } } } } } Print(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); return 0; } |