#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 2'005;
bool vis[MAXN][MAXN];
char plansza[MAXN][MAXN];
set<char> idk[MAXN * 2];
map<pair<int, char>, int> cnt;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
char c;
cin >> c;
plansza[i][j] = c;
idk[i].insert(c);
cnt[{i, c}]++;
idk[MAXN + j].insert(c);
cnt[{MAXN + j, c}]++;
}
}
bool lol = 1;
stack<pair<int, char>> ans;
set<char>::iterator it;
while (lol)
{
lol = 0;
for (int i = 0; i < m; i++)
{
if (idk[MAXN + i].size() == 1)
{
it = idk[MAXN + i].begin();
ans.push({MAXN + i, *it});
lol = 1;
for (int j = 0; j < n; j++)
{
if (vis[j][i])
continue;
vis[j][i] = 1;
cnt[{j, plansza[j][i]}]--;
if (!cnt[{j, plansza[j][i]}])
{
it = idk[j].find(plansza[j][i]);
idk[j].erase(it);
}
}
idk[MAXN + i].clear();
}
}
for (int i = 0; i < n; i++)
{
if (idk[i].size() == 1)
{
it = idk[i].begin();
ans.push({i, *it});
lol = 1;
for (int j = 0; j < m; j++)
{
if (vis[i][j])
continue;
vis[i][j] = 1;
cnt[{MAXN + j, plansza[i][j]}]--;
if (!cnt[{MAXN + j, plansza[i][j]}])
{
it = idk[MAXN + j].find(plansza[i][j]);
idk[MAXN + j].erase(it);
}
}
idk[i].clear();
}
}
}
cout << ans.size() << '\n';
while (ans.size())
{
int a = ans.top().first;
char c = ans.top().second;
ans.pop();
if (a >= MAXN)
{
a -= MAXN;
cout << "K ";
}
else
cout << "R ";
cout << a + 1 << ' ' << c << '\n';
}
// for (int i = 0; i < n; i++)
// {
// for (auto x : idk[i])
// cout << x << ' ';
// cout << '\n';
// }
// for (int i = 0; i < m; i++)
// {
// for (auto x : idk[MAXN + i])
// cout << x << ' ';
// cout << '\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 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 109 110 111 112 113 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 2'005; bool vis[MAXN][MAXN]; char plansza[MAXN][MAXN]; set<char> idk[MAXN * 2]; map<pair<int, char>, int> cnt; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; plansza[i][j] = c; idk[i].insert(c); cnt[{i, c}]++; idk[MAXN + j].insert(c); cnt[{MAXN + j, c}]++; } } bool lol = 1; stack<pair<int, char>> ans; set<char>::iterator it; while (lol) { lol = 0; for (int i = 0; i < m; i++) { if (idk[MAXN + i].size() == 1) { it = idk[MAXN + i].begin(); ans.push({MAXN + i, *it}); lol = 1; for (int j = 0; j < n; j++) { if (vis[j][i]) continue; vis[j][i] = 1; cnt[{j, plansza[j][i]}]--; if (!cnt[{j, plansza[j][i]}]) { it = idk[j].find(plansza[j][i]); idk[j].erase(it); } } idk[MAXN + i].clear(); } } for (int i = 0; i < n; i++) { if (idk[i].size() == 1) { it = idk[i].begin(); ans.push({i, *it}); lol = 1; for (int j = 0; j < m; j++) { if (vis[i][j]) continue; vis[i][j] = 1; cnt[{MAXN + j, plansza[i][j]}]--; if (!cnt[{MAXN + j, plansza[i][j]}]) { it = idk[MAXN + j].find(plansza[i][j]); idk[MAXN + j].erase(it); } } idk[i].clear(); } } } cout << ans.size() << '\n'; while (ans.size()) { int a = ans.top().first; char c = ans.top().second; ans.pop(); if (a >= MAXN) { a -= MAXN; cout << "K "; } else cout << "R "; cout << a + 1 << ' ' << c << '\n'; } // for (int i = 0; i < n; i++) // { // for (auto x : idk[i]) // cout << x << ' '; // cout << '\n'; // } // for (int i = 0; i < m; i++) // { // for (auto x : idk[MAXN + i]) // cout << x << ' '; // cout << '\n'; // } return 0; } |
English