#include <bits/stdc++.h>
using namespace std;
class block{
int chars[26];
int ilosc_roznych=0;
bool kolored=0;
public:
void add(char x)
{
chars[x-'A']++;
if(chars[x-'A']==1)ilosc_roznych++;
}
char erase(char x)
{
chars[x-'A']--;
if(chars[x-'A']==0)
ilosc_roznych--;
if(kolored)
return '-';
if(ilosc_roznych==1)
{
for(int i = 0 ;i <26 ;i ++)
if(chars[i]>0){
kolored=1;
return i+'A';
}
}
return '-';
}
char czy_kolorowac(int n)
{
if(kolored)return '-';
for(int i = 0 ;i < 26 ; i ++)
if(chars[i]==n){
kolored=1;
return i+'A';
}
return '-';
}
};
block rzedy[2100];
block kolumny[2100];
int main()
{
int n,m;
cin>>n>>m;
for(int i = 1 ;i <= n ;i ++)
{
for(int j = 1; j<=m ;j ++)
{
char x;
cin>>x;
kolumny[j].add(x);
rzedy[i].add(x);
}
}
queue<pair<pair<char,int>,int>> Q;
for(int i = 1; i<= m ;i ++){
char x=kolumny[i].czy_kolorowac(n);
if(x!='-')
Q.push({{'K',i},x});
}
for(int i = 1; i<= n ;i ++){
char x=rzedy[i].czy_kolorowac(m);
if(x!='-')
Q.push({{'R',i},x});
}
stack<pair<pair<char,int>,int>> S;
while(!Q.empty())
{
auto I = Q.front();
Q.pop();
char bok=I.first.first,znak=I.second;
int pos=I.first.second;
S.push(I);
if(bok=='R')
{
for(int i = 1; i<=m ;i ++)
{
char x=kolumny[i].erase(znak);
if(x!='-')
Q.push({{'K',i},x});
}
}
if(bok=='K')
{
for(int i = 1; i<=n ;i ++)
{
char x=rzedy[i].erase(znak);
if(x!='-')
Q.push({{'R',i},x});
}
}
}
cout<<S.size()<<'\n';
while(!S.empty())
{
auto I = S.top();
S.pop();
cout<<I.first.first<<' '<<I.first.second<<' '<<char(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; class block{ int chars[26]; int ilosc_roznych=0; bool kolored=0; public: void add(char x) { chars[x-'A']++; if(chars[x-'A']==1)ilosc_roznych++; } char erase(char x) { chars[x-'A']--; if(chars[x-'A']==0) ilosc_roznych--; if(kolored) return '-'; if(ilosc_roznych==1) { for(int i = 0 ;i <26 ;i ++) if(chars[i]>0){ kolored=1; return i+'A'; } } return '-'; } char czy_kolorowac(int n) { if(kolored)return '-'; for(int i = 0 ;i < 26 ; i ++) if(chars[i]==n){ kolored=1; return i+'A'; } return '-'; } }; block rzedy[2100]; block kolumny[2100]; int main() { int n,m; cin>>n>>m; for(int i = 1 ;i <= n ;i ++) { for(int j = 1; j<=m ;j ++) { char x; cin>>x; kolumny[j].add(x); rzedy[i].add(x); } } queue<pair<pair<char,int>,int>> Q; for(int i = 1; i<= m ;i ++){ char x=kolumny[i].czy_kolorowac(n); if(x!='-') Q.push({{'K',i},x}); } for(int i = 1; i<= n ;i ++){ char x=rzedy[i].czy_kolorowac(m); if(x!='-') Q.push({{'R',i},x}); } stack<pair<pair<char,int>,int>> S; while(!Q.empty()) { auto I = Q.front(); Q.pop(); char bok=I.first.first,znak=I.second; int pos=I.first.second; S.push(I); if(bok=='R') { for(int i = 1; i<=m ;i ++) { char x=kolumny[i].erase(znak); if(x!='-') Q.push({{'K',i},x}); } } if(bok=='K') { for(int i = 1; i<=n ;i ++) { char x=rzedy[i].erase(znak); if(x!='-') Q.push({{'R',i},x}); } } } cout<<S.size()<<'\n'; while(!S.empty()) { auto I = S.top(); S.pop(); cout<<I.first.first<<' '<<I.first.second<<' '<<char(I.second)<<'\n'; } } |
English