#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#define MAKS 2010
using namespace std;
char tab[MAKS][MAKS];
int cnt[2][MAKS][30];
int roznych[2][MAKS];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)scanf("%s",&tab[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cnt[0][i][tab[i][j]-'A']++;
cnt[1][j][tab[i][j]-'A']++;
}
}
queue< pair<int,int> > q;
for(int i=0;i<n;i++)
{
for(char c='A';c<='Z';c++)
{
if(cnt[0][i][c-'A']>0)roznych[0][i]++;
}
if(roznych[0][i]==1)q.push(make_pair(0,i));
}
for(int j=0;j<m;j++)
{
for(char c='A';c<='Z';c++)
{
if(cnt[1][j][c-'A']>0)roznych[1][j]++;
}
if(roznych[1][j]==1)q.push(make_pair(1,j));
}
vector< pair< pair<int,int>, char> > wynik;
while(!q.empty())
{
pair<int,int> p=q.front(); q.pop();
int typ=p.first; int x=p.second;
char w='?';
for(char c='A';c<='Z';c++)
{
if(cnt[typ][x][c-'A']>0)
{
w=c;
break;
}
}
wynik.push_back(make_pair(make_pair(typ, x),w));
cnt[typ][x][w-'A']=0;
roznych[typ][x]=0;
if(typ==0)
{
for(int j=0;j<m;j++)
{
if(roznych[1][j]<=1)continue;
cnt[1][j][w-'A']--;
if(cnt[1][j][w-'A']==0)roznych[1][j]--;
if(roznych[1][j]==1)q.push(make_pair(1,j));
}
}
else
{
for(int i=0;i<n;i++)
{
if(roznych[0][i]<=1)continue;
cnt[0][i][w-'A']--;
if(cnt[0][i][w-'A']==0)roznych[0][i]--;
if(roznych[0][i]==1)q.push(make_pair(0,i));
}
}
}
printf("%d\n",int(wynik.size()));
for(int i=wynik.size()-1;i>=0;i--)
{
printf("%c %d %c\n",(wynik[i].first.first==0 ? 'R': 'K'), wynik[i].first.second+1, wynik[i].second);
}
}
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 | #include <cstdio> #include <vector> #include <algorithm> #include <queue> #define MAKS 2010 using namespace std; char tab[MAKS][MAKS]; int cnt[2][MAKS][30]; int roznych[2][MAKS]; int main() { int n,m; scanf("%d %d",&n,&m); for(int i=0;i<n;i++)scanf("%s",&tab[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cnt[0][i][tab[i][j]-'A']++; cnt[1][j][tab[i][j]-'A']++; } } queue< pair<int,int> > q; for(int i=0;i<n;i++) { for(char c='A';c<='Z';c++) { if(cnt[0][i][c-'A']>0)roznych[0][i]++; } if(roznych[0][i]==1)q.push(make_pair(0,i)); } for(int j=0;j<m;j++) { for(char c='A';c<='Z';c++) { if(cnt[1][j][c-'A']>0)roznych[1][j]++; } if(roznych[1][j]==1)q.push(make_pair(1,j)); } vector< pair< pair<int,int>, char> > wynik; while(!q.empty()) { pair<int,int> p=q.front(); q.pop(); int typ=p.first; int x=p.second; char w='?'; for(char c='A';c<='Z';c++) { if(cnt[typ][x][c-'A']>0) { w=c; break; } } wynik.push_back(make_pair(make_pair(typ, x),w)); cnt[typ][x][w-'A']=0; roznych[typ][x]=0; if(typ==0) { for(int j=0;j<m;j++) { if(roznych[1][j]<=1)continue; cnt[1][j][w-'A']--; if(cnt[1][j][w-'A']==0)roznych[1][j]--; if(roznych[1][j]==1)q.push(make_pair(1,j)); } } else { for(int i=0;i<n;i++) { if(roznych[0][i]<=1)continue; cnt[0][i][w-'A']--; if(cnt[0][i][w-'A']==0)roznych[0][i]--; if(roznych[0][i]==1)q.push(make_pair(0,i)); } } } printf("%d\n",int(wynik.size())); for(int i=wynik.size()-1;i>=0;i--) { printf("%c %d %c\n",(wynik[i].first.first==0 ? 'R': 'K'), wynik[i].first.second+1, wynik[i].second); } } |
English