// #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back // #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 2000; int a, b, f[N + 5][2], st[N + 5][2][256]; char c[N + 5][N + 5], s[N + 5][2], res[N + 5][2]; bool vis[N + 5][2]; queue<pair<int, int>> qu; vector<pair<int, int>> ans; bool ck(int n, int m) { return __builtin_popcount(f[n][m]) <= 1; } #define MP make_pair signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); // ios::sync_with_stdio(0); // cin.tie(0), cout.tie(0); scanf("%d%d", &a, &b); // cin >> a >> b; rep(i, 1, a) scanf("%s", c[i] + 1); rep(i, 1, a) rep(j, 1, b) { if(!(st[i][0][c[i][j]]++)) f[i][0] |= 1 << (c[i][j] - 'A'); if(!(st[j][1][c[i][j]]++)) f[j][1] |= 1 << (c[i][j] - 'A'); } rep(i, 1, a) if(ck(i, 0)) qu.push(MP(i, 0)); rep(i, 1, b) if(ck(i, 1)) qu.push(MP(i, 1)); while(siz(qu)) { auto [x, y] = qu.front(); qu.pop(); if(!f[x][y] || vis[x][y]) continue; // cout << x << ' ' << y << endl; vis[x][y] = 1; res[x][y] = __lg(f[x][y]) + 'A'; ans.PB(x, y); if(!y) { rep(j, 1, b) if(c[x][j]) { if(!(--st[j][1][c[x][j]])) f[j][1] ^= 1 << (c[x][j] - 'A'); if(ck(j, 1)) qu.push(MP(j, 1)); c[x][j] = 0; } } else { rep(i, 1, a) if(c[i][x]) { if(!(--st[i][0][c[i][x]])) f[i][0] ^= 1 << (c[i][x] - 'A'); if(ck(i, 0)) qu.push(MP(i, 0)); c[i][x] = 0; } } } reverse(ans.begin(), ans.end()); cout << siz(ans) << '\n'; for(auto [x, y] : ans) { cout << (y ? 'K' : 'R') << ' ' << x << ' ' << res[x][y] << '\n'; } return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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 | // #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back // #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 2000; int a, b, f[N + 5][2], st[N + 5][2][256]; char c[N + 5][N + 5], s[N + 5][2], res[N + 5][2]; bool vis[N + 5][2]; queue<pair<int, int>> qu; vector<pair<int, int>> ans; bool ck(int n, int m) { return __builtin_popcount(f[n][m]) <= 1; } #define MP make_pair signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); // ios::sync_with_stdio(0); // cin.tie(0), cout.tie(0); scanf("%d%d", &a, &b); // cin >> a >> b; rep(i, 1, a) scanf("%s", c[i] + 1); rep(i, 1, a) rep(j, 1, b) { if(!(st[i][0][c[i][j]]++)) f[i][0] |= 1 << (c[i][j] - 'A'); if(!(st[j][1][c[i][j]]++)) f[j][1] |= 1 << (c[i][j] - 'A'); } rep(i, 1, a) if(ck(i, 0)) qu.push(MP(i, 0)); rep(i, 1, b) if(ck(i, 1)) qu.push(MP(i, 1)); while(siz(qu)) { auto [x, y] = qu.front(); qu.pop(); if(!f[x][y] || vis[x][y]) continue; // cout << x << ' ' << y << endl; vis[x][y] = 1; res[x][y] = __lg(f[x][y]) + 'A'; ans.PB(x, y); if(!y) { rep(j, 1, b) if(c[x][j]) { if(!(--st[j][1][c[x][j]])) f[j][1] ^= 1 << (c[x][j] - 'A'); if(ck(j, 1)) qu.push(MP(j, 1)); c[x][j] = 0; } } else { rep(i, 1, a) if(c[i][x]) { if(!(--st[i][0][c[i][x]])) f[i][0] ^= 1 << (c[i][x] - 'A'); if(ck(i, 0)) qu.push(MP(i, 0)); c[i][x] = 0; } } } reverse(ans.begin(), ans.end()); cout << siz(ans) << '\n'; for(auto [x, y] : ans) { cout << (y ? 'K' : 'R') << ' ' << x << ' ' << res[x][y] << '\n'; } return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0; } |