/*
Student name: Mislav Bradac
School name: V. gimnazija, Zagreb, Croatia
Division: Senior
Contest: #4
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int DEBUG = 0, DEBUG2 = 0, DEBUG3 = 0;
const string BROJ = "A23456789TJQK", BOJA = "DCHS";
typedef pair<int,int> par;
par get(char *p) {
par t = par( BROJ.find( string(1, p[0]) ), BOJA.find(
string(1, p[1]) ) );
return t;
}
int hand[20][20], hand_copy[20][20];
vector<par> run[2], sets[2];
void output (vector<par> &V) {
if (V.size() == 0) return;
if (DEBUG) printf ("********* %d
%d\n", V[0].first, V[0].second);
for (int i = 0; i < (int)V.size();
++i) printf ("%c%c ", BROJ[V[i].second], BOJA[V[i].first]);
V.clear();
}
int find_runs_and_sets() {
int ok = 0;
for (int i = 0; i < 4; ++i)
{
int cnt = 0;
for (int j = 0; j < 15; ++j)
{
if (hand[i][j]) ++cnt;
if (!hand[i][j] || cnt == 4)
{
if (cnt >= 3) {
int idx =
(run[0].size() ? 1 : 0);
for (int k = j - cnt
+ (cnt == 4); k < j + (cnt == 4); ++k) {
run[idx].push_back( par(i, k) );
hand[i][k] =
0;
}
ok = 1;
}
cnt = 0;
}
}
}
for (int i = 0; i < 15; ++i)
{
int cnt = 0;
for (int j = 0; j < 4; ++j)
cnt += hand[j][i];
if (cnt >= 3) {
if (DEBUG) { printf
("dada\n"); fflush(stdout); }
int idx = (sets[0].size() ? 1
: 0);
for (int k = 3; k >= 0;
--k) if (hand[k][i]) {
sets[idx].push_back(
par(k, i) );
hand[k][i] = 0;
}
ok = 1;
if (DEBUG) {
output(sets[idx]); printf("\n"); fflush(stdout); }
}
}
return ok;
}
void erase_smallest () {
if (DEBUG) { printf
("bio\n"); fflush(stdout); }
for (int i = 0; i < 15; ++i) for
(int j = 0; j < 4; ++j) if (hand[j][i]) { hand[j][i] = 0; return; }
}
bool victory () {
return (run[0].size() ? 1 : 0) +
(run[1].size() ? 1 : 0) + (sets[0].size() ? 1 : 0) + (sets[1].size() ? 1 : 0)
== 2;
}
bool operator > (const par &A, const par &B) { return A.second !=
B.second ? A.second > B.second : A.first > B.first; }
bool operator > (const vector<par> &A, const vector<par>
&B) {
if (A.size() == 0 || B.size() == 0)
return 0;
return A[0] > B[0];
}
void output () {
if (run[0].size() == 4) output(run[0]);
if (run[1].size() == 4)
output(run[1]);
if (sets[0].size() == 4)
output(sets[0]);
if (sets[1].size() == 4)
output(sets[1]);
if (run[0] > run[1]) swap(run[0],
run[1]);
output(run[0]); output(run[1]);
if (sets[0] > sets[1]) swap(sets[0],
sets[1]);
output(sets[0]);
output(sets[1]);
for (int i = 15; i >= 0; --i) for
(int j = 3; j >= 0; --j) if (hand[j][i]) printf ("%c%c ", BROJ[i],
BOJA[j]);
printf ("\n");
if (DEBUG2) { printf
("skajfklakjlds\n"); }
}
int update_runs_and_sets(par t) {
swap (t.second, t.first);
for (int i = 0; i < 2; ++i)
{
if (run[i].size() == 0 ||
run[i].size() == 4) continue;
if (run[i][0].first != t.first)
continue;
if (run[i][0].second - 1 ==
t.second) { run[i].insert( run[i].begin(), t ); return 1; }
if (run[i].back().second + 1 ==
t.second) { run[i].push_back(t); return 1; }
}
for (int i = 0; i < 2; ++i)
{
if (!sets[i].size())
continue;
if (sets[i][0].second !=
t.second) continue;
sets[i].clear();
for (int j = 3; j >= 0; --j)
sets[i].push_back( par(j, t.second) );
return 1;
}
return 0;
}
int main ()
{
for (int i = 0; i < 7; ++i)
{
char buff[5];
scanf ("%s",
buff);
par t = get(buff);
//printf ("** %d %d\n",
t.first, t.second);
hand[t.second][t.first] =
1;
}
memcpy (hand_copy, hand, sizeof
hand);
for (int _i = 0; _i < 5; ++_i)
{
int ok = 0;
run[0].clear(); run[1].clear();
sets[0].clear(); sets[1].clear();
memcpy (hand, hand_copy, sizeof
hand_copy);
find_runs_and_sets();
if (victory()) { output(); ok =
1; }
//if (DEBUG) { printf
("pero\n"); fflush(stdout); }
//if (DEBUG3) { output(run[0]);
printf ("\n"); output(run[1]); printf("\n");
output(sets[0]); printf("\n"); output(sets[1]);
printf("\n"); fflush(stdout); }
for (int i = 0; i < 5; ++i)
{
if (DEBUG3) {
for (int i = 0; i <
(int)sets[1].size(); ++i) printf ("*%d %d\n", sets[0][i].first,
sets[0][i].second);
}
if (DEBUG) { printf
("*** %d\n", i); fflush(stdout); }
char buff[5];
scanf ("%s",
buff);
if (ok) continue;
par t = get(buff);
if (update_runs_and_sets(t))
{
erase_smallest();
if (victory()) {
output(); ok = 1; }
}
else {
if (DEBUG) { printf
("*** %d\n", i); fflush(stdout); }
hand[t.second][t.first] =
1;
if (find_runs_and_sets())
{
erase_smallest();
if (victory()) {
output(); ok = 1; }
}
hand[t.second][t.first] =
0;
}
}
if (!ok) output();
}
system("pause");
return 0;
}