#include <stdio.h>
void counterTest(char *str)
{
//make table
char cards[4][14];
int countByColumn[14] = {0};
int countByRow[4] = {0};
char *str_ = str;
while (*str_ != 0) {
char mark = 0, num = 0;
switch (*str_) {
case 'D': mark = 0; break;
case 'H': mark = 1; break;
case 'S': mark = 2; break;
case 'C': mark = 3; break;
default: printf("illegal character:%c\n", *str_
); return; }
str_++;
if (*str_
== 0) { printf("illegal length:%lu\n", strlen(str
)); return; }
if (*str_ >= '2' && *str_ <= '9') {
num = *str_ - '0';
}
else {
switch (*str_) {
case 'A': num = 1; break;
case 'T': num = 10; break;
case 'J': num = 11; break;
case 'Q': num = 12; break;
case 'K': num = 13; break;
default: { printf("illegal character:%c\n", *str_
); return; } }
}
cards[mark][num] = 'Y';
countByColumn[num]++;
countByRow[mark]++;
str_++;
}
//markup D and I
markup_D_and_I:
for (int c=1; c<14; c++) {
if (countByColumn[c] == 1) {
for (int r=0; r<4; r++) {
if (cards[r][c] == 'Y') {
if (countByRow[r] == 1) {
cards[r][c] = 'I';
}
else {
cards[r][c] = 'D';
}
break;
}
}
}
}
for (int r=0; r<4; r++) {
if (countByRow[r] == 1) {
for (int c=0; c<14; c++) {
if (cards[r][c] == 'Y') {
if (countByColumn[c] == 1) {
cards[r][c] = 'I';
}
else {
cards[r][c] = 'D';
}
break;
}
}
}
}
//find D pair
for (int c=1; c<14; c++) {
for (int r=0; r<4; r++) {
if (cards[r][c] == 'D') {
char *card = &cards[r][c];
if (countByColumn[c] == 1) {
for (int c_=1; c_<14; c_++) {
if (&cards[r][c_] != card && cards[r][c_] == 'D') {
cards[r][c] = cards[r][c_] = 'P';
countByColumn[c]--;
countByColumn[c_]--;
countByRow[r]-=2;
goto markup_D_and_I;
}
}
//make pair with Y
for (int c_=1; c_<14; c_++) {
if (&cards[r][c_] != card && cards[r][c_] == 'Y') {
cards[r][c] = cards[r][c_] = 'P';
countByColumn[c]--;
countByColumn[c_]--;
countByRow[r]-=2;
goto markup_D_and_I;
}
}
//error no found pair
}
else {
for (int r_=0; r_<4; r_++) {
if (&cards[r_][c] != card && cards[r_][c] == 'D') {
cards[r][c] = cards[r_][c] = 'P';
countByRow[r]--;
countByRow[r_]--;
countByColumn[c]-=2;
goto markup_D_and_I;
}
}
//make pair with Y
for (int r_=0; r_<4; r_++) {
if (&cards[r_][c] != card && cards[r_][c] == 'Y') {
cards[r][c] = cards[r_][c] = 'P';
countByRow[r]--;
countByRow[r_]--;
countByColumn[c]-=2;
goto markup_D_and_I;
}
}
//error no found pair
}
}
}
}
//brute force
for (int c=1; c<14; c++) {
for (int r=0; r<4; r++) {
if (cards[r][c] == 'Y') {
//make pair with Y
char *card = &cards[r][c];
for (int c_=1; c_<14; c_++) {
if (&cards[r][c_] != card && cards[r][c_] == 'Y') {
cards[r][c] = cards[r][c_] = 'P';
countByColumn[c]--;
countByColumn[c_]--;
countByRow[r]-=2;
goto markup_D_and_I;
}
}
for (int r_=0; r_<4; r_++) {
if (&cards[r_][c] != card && cards[r_][c] == 'Y') {
cards[r][c] = cards[r_][c] = 'P';
countByRow[r]--;
countByRow[r_]--;
countByColumn[c]-=2;
goto markup_D_and_I;
}
}
//error no found pair
}
}
}
//count up P
int countPair = 0;
for (int c=1; c<14; c++) {
for (int r=0; r<4; r++) {
if (cards[r][c] == 'P') {
countPair++;
}
}
}
//result
printf("%s %dpairs\n", str
, countPair
/2); }
int main(void) {
char *a = "DAD2HAH3"; //2
char *b = "DAD3D8D9DJH5H8HKSASKCAC5C8CK"; //7
char *c = "SAS2S3S4S5S6S7S8S9C2C3C4C5C6C7C8C9CT"; //9
char *d = "D2H2S2C2DK"; //2
char *e = "HAH2H3H4H5H6H7H8H9HTHJHQHKCAC2C3C4C5C6C7C8C9CTCJCQCK"; //13
char *f = "HAH2C2CAS7SAD8DA"; //4
counterTest(a);
counterTest(b);
counterTest(c);
counterTest(d);
counterTest(e);
counterTest(f);
return 0;
}