#include<bits/stdc++.h>
using namespace std;
constexpr int ALFA = 26;
constexpr int N=2e3+7;
int kolumny[N][ALFA];
int wiersze[N][ALFA];
int ileK[N], ileW[N];
int obraz[N][N];
int literaki[ALFA];
int n,m;
char c;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int y=0;y<n;y++)
{
for(int x=0;x<m;x++)
{
cin >> c;
c-='A';
obraz[x][y]=(int)c;
}
}
// for(int y=0;y<n;y++)
// {
// for(int x=0;x<m;x++)
// {
// cout.width(2);
// cout << (char)(obraz[x][y]+'A') << " ";
// }
// cout << "\n";
// }
for(int y=0;y<n;y++)
{
for(int i=0;i<ALFA;i++)
literaki[i]=0;
for(int x=0;x<m;x++)
{
literaki[obraz[x][y]]=1;
wiersze[y][obraz[x][y]]++;
}
for(int i=0;i<ALFA;i++)
if(literaki[i])
ileW[y]++;
}
for(int x=0;x<m;x++)
{
for(int i=0;i<ALFA;i++)
literaki[i]=0;
for(int y=0;y<n;y++)
{
literaki[obraz[x][y]]++;
kolumny[x][obraz[x][y]]++;
}
for(int i=0;i<ALFA;i++)
{
if(literaki[i])
ileK[x]++;
}
}
// for(int x=0;x<m;x++)
// cout << maxK[x] << " ";
// cout << "\n";
// for(int y=0;y<n;y++)
// cout << maxW[y] << " ";
// cout << "\n";
queue<int> que;
for(int x=0;x<m;x++)
if(ileK[x]==1)
que.push(x);
for(int y=0;y<n;y++)
if(ileW[y]==1)
que.push(y+m);
stack<pair<char,int>> wyn;
int act;
while(!que.empty())
{
act = que.front();
que.pop();
if(act>=m)
{
act-=m;
if(ileW[act]==0)
continue;
ileW[act]=0;
for(int i=0;i<ALFA;i++)
{
if(wiersze[act][i]!=0)
{
wyn.push({i,act+m});
break;
}
}
for(int x=0;x<m;x++)
{
if(obraz[x][act]==-1)
continue;
kolumny[x][obraz[x][act]]--;
if(kolumny[x][obraz[x][act]]==0)
ileK[x]--;
if(ileK[x]==1)
que.push(x);
obraz[x][act]=-1;
}
}
else
{
if(ileK[act]==0)
continue;
ileK[act]=0;
for(int i=0;i<ALFA;i++)
{
if(kolumny[act][i]!=0)
{
wyn.push({i,act});
break;
}
}
for(int y=0;y<n;y++)
{
if(obraz[act][y]==-1)
continue;
wiersze[y][obraz[act][y]]--;
if(wiersze[y][obraz[act][y]]==0)
ileW[y]--;
if(ileW[y]==1)
que.push(y+m);
obraz[act][y]=-1;
}
}
}
cout << wyn.size() << "\n";
while(!wyn.empty())
{
if(wyn.top().second>=m)
cout << "R "<< wyn.top().second-m+1 << " " << (char)((char)wyn.top().first+'A') << "\n";
else
cout << "K "<< wyn.top().second+1 << " " << (char)((char)wyn.top().first+'A') << "\n";
wyn.pop();
}
}
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include<bits/stdc++.h> using namespace std; constexpr int ALFA = 26; constexpr int N=2e3+7; int kolumny[N][ALFA]; int wiersze[N][ALFA]; int ileK[N], ileW[N]; int obraz[N][N]; int literaki[ALFA]; int n,m; char c; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int y=0;y<n;y++) { for(int x=0;x<m;x++) { cin >> c; c-='A'; obraz[x][y]=(int)c; } } // for(int y=0;y<n;y++) // { // for(int x=0;x<m;x++) // { // cout.width(2); // cout << (char)(obraz[x][y]+'A') << " "; // } // cout << "\n"; // } for(int y=0;y<n;y++) { for(int i=0;i<ALFA;i++) literaki[i]=0; for(int x=0;x<m;x++) { literaki[obraz[x][y]]=1; wiersze[y][obraz[x][y]]++; } for(int i=0;i<ALFA;i++) if(literaki[i]) ileW[y]++; } for(int x=0;x<m;x++) { for(int i=0;i<ALFA;i++) literaki[i]=0; for(int y=0;y<n;y++) { literaki[obraz[x][y]]++; kolumny[x][obraz[x][y]]++; } for(int i=0;i<ALFA;i++) { if(literaki[i]) ileK[x]++; } } // for(int x=0;x<m;x++) // cout << maxK[x] << " "; // cout << "\n"; // for(int y=0;y<n;y++) // cout << maxW[y] << " "; // cout << "\n"; queue<int> que; for(int x=0;x<m;x++) if(ileK[x]==1) que.push(x); for(int y=0;y<n;y++) if(ileW[y]==1) que.push(y+m); stack<pair<char,int>> wyn; int act; while(!que.empty()) { act = que.front(); que.pop(); if(act>=m) { act-=m; if(ileW[act]==0) continue; ileW[act]=0; for(int i=0;i<ALFA;i++) { if(wiersze[act][i]!=0) { wyn.push({i,act+m}); break; } } for(int x=0;x<m;x++) { if(obraz[x][act]==-1) continue; kolumny[x][obraz[x][act]]--; if(kolumny[x][obraz[x][act]]==0) ileK[x]--; if(ileK[x]==1) que.push(x); obraz[x][act]=-1; } } else { if(ileK[act]==0) continue; ileK[act]=0; for(int i=0;i<ALFA;i++) { if(kolumny[act][i]!=0) { wyn.push({i,act}); break; } } for(int y=0;y<n;y++) { if(obraz[act][y]==-1) continue; wiersze[y][obraz[act][y]]--; if(wiersze[y][obraz[act][y]]==0) ileW[y]--; if(ileW[y]==1) que.push(y+m); obraz[act][y]=-1; } } } cout << wyn.size() << "\n"; while(!wyn.empty()) { if(wyn.top().second>=m) cout << "R "<< wyn.top().second-m+1 << " " << (char)((char)wyn.top().first+'A') << "\n"; else cout << "K "<< wyn.top().second+1 << " " << (char)((char)wyn.top().first+'A') << "\n"; wyn.pop(); } } |
English