fork download
  1. #include <stdio.h>
  2.  
  3. void counterTest(char *str)
  4. {
  5. //make table
  6. char cards[4][14];
  7. memset(cards, '_', 4*14);
  8. int countByColumn[14] = {0};
  9. int countByRow[4] = {0};
  10.  
  11. char *str_ = str;
  12. while (*str_ != 0) {
  13. char mark = 0, num = 0;
  14. switch (*str_) {
  15. case 'D': mark = 0; break;
  16. case 'H': mark = 1; break;
  17. case 'S': mark = 2; break;
  18. case 'C': mark = 3; break;
  19. default: printf("illegal character:%c\n", *str_); return;
  20. }
  21. str_++;
  22. if (*str_ == 0) { printf("illegal length:%lu\n", strlen(str)); return; }
  23.  
  24. if (*str_ >= '2' && *str_ <= '9') {
  25. num = *str_ - '0';
  26. }
  27. else {
  28. switch (*str_) {
  29. case 'A': num = 1; break;
  30. case 'T': num = 10; break;
  31. case 'J': num = 11; break;
  32. case 'Q': num = 12; break;
  33. case 'K': num = 13; break;
  34. default: { printf("illegal character:%c\n", *str_); return; }
  35. }
  36. }
  37. cards[mark][num] = 'Y';
  38. countByColumn[num]++;
  39. countByRow[mark]++;
  40. str_++;
  41. }
  42.  
  43. //markup D and I
  44. markup_D_and_I:
  45.  
  46. for (int c=1; c<14; c++) {
  47. if (countByColumn[c] == 1) {
  48. for (int r=0; r<4; r++) {
  49. if (cards[r][c] == 'Y') {
  50. if (countByRow[r] == 1) {
  51. cards[r][c] = 'I';
  52. }
  53. else {
  54. cards[r][c] = 'D';
  55. }
  56. break;
  57. }
  58. }
  59. }
  60. }
  61.  
  62. for (int r=0; r<4; r++) {
  63. if (countByRow[r] == 1) {
  64. for (int c=0; c<14; c++) {
  65. if (cards[r][c] == 'Y') {
  66. if (countByColumn[c] == 1) {
  67. cards[r][c] = 'I';
  68. }
  69. else {
  70. cards[r][c] = 'D';
  71. }
  72. break;
  73. }
  74. }
  75. }
  76. }
  77.  
  78. //find D pair
  79. for (int c=1; c<14; c++) {
  80. for (int r=0; r<4; r++) {
  81. if (cards[r][c] == 'D') {
  82. char *card = &cards[r][c];
  83. if (countByColumn[c] == 1) {
  84. for (int c_=1; c_<14; c_++) {
  85. if (&cards[r][c_] != card && cards[r][c_] == 'D') {
  86. cards[r][c] = cards[r][c_] = 'P';
  87. countByColumn[c]--;
  88. countByColumn[c_]--;
  89. countByRow[r]-=2;
  90. goto markup_D_and_I;
  91. }
  92. }
  93.  
  94. //make pair with Y
  95. for (int c_=1; c_<14; c_++) {
  96. if (&cards[r][c_] != card && cards[r][c_] == 'Y') {
  97. cards[r][c] = cards[r][c_] = 'P';
  98. countByColumn[c]--;
  99. countByColumn[c_]--;
  100. countByRow[r]-=2;
  101. goto markup_D_and_I;
  102. }
  103. }
  104.  
  105. //error no found pair
  106. }
  107. else {
  108. for (int r_=0; r_<4; r_++) {
  109. if (&cards[r_][c] != card && cards[r_][c] == 'D') {
  110. cards[r][c] = cards[r_][c] = 'P';
  111. countByRow[r]--;
  112. countByRow[r_]--;
  113. countByColumn[c]-=2;
  114. goto markup_D_and_I;
  115. }
  116. }
  117.  
  118. //make pair with Y
  119. for (int r_=0; r_<4; r_++) {
  120. if (&cards[r_][c] != card && cards[r_][c] == 'Y') {
  121. cards[r][c] = cards[r_][c] = 'P';
  122. countByRow[r]--;
  123. countByRow[r_]--;
  124. countByColumn[c]-=2;
  125. goto markup_D_and_I;
  126. }
  127. }
  128.  
  129. //error no found pair
  130. }
  131. }
  132. }
  133. }
  134.  
  135. //brute force
  136. for (int c=1; c<14; c++) {
  137. for (int r=0; r<4; r++) {
  138. if (cards[r][c] == 'Y') {
  139. //make pair with Y
  140. char *card = &cards[r][c];
  141. for (int c_=1; c_<14; c_++) {
  142. if (&cards[r][c_] != card && cards[r][c_] == 'Y') {
  143. cards[r][c] = cards[r][c_] = 'P';
  144. countByColumn[c]--;
  145. countByColumn[c_]--;
  146. countByRow[r]-=2;
  147. goto markup_D_and_I;
  148. }
  149. }
  150.  
  151. for (int r_=0; r_<4; r_++) {
  152. if (&cards[r_][c] != card && cards[r_][c] == 'Y') {
  153. cards[r][c] = cards[r_][c] = 'P';
  154. countByRow[r]--;
  155. countByRow[r_]--;
  156. countByColumn[c]-=2;
  157. goto markup_D_and_I;
  158. }
  159. }
  160.  
  161. //error no found pair
  162. }
  163. }
  164. }
  165.  
  166. //count up P
  167. int countPair = 0;
  168. for (int c=1; c<14; c++) {
  169. for (int r=0; r<4; r++) {
  170. if (cards[r][c] == 'P') {
  171. countPair++;
  172. }
  173. }
  174. }
  175.  
  176. //result
  177. printf("%s %dpairs\n", str, countPair/2);
  178. }
  179.  
  180. int main(void) {
  181. char *a = "DAD2HAH3"; //2
  182. char *b = "DAD3D8D9DJH5H8HKSASKCAC5C8CK"; //7
  183. char *c = "SAS2S3S4S5S6S7S8S9C2C3C4C5C6C7C8C9CT"; //9
  184. char *d = "D2H2S2C2DK"; //2
  185. char *e = "HAH2H3H4H5H6H7H8H9HTHJHQHKCAC2C3C4C5C6C7C8C9CTCJCQCK"; //13
  186. char *f = "HAH2C2CAS7SAD8DA"; //4
  187.  
  188. counterTest(a);
  189. counterTest(b);
  190. counterTest(c);
  191. counterTest(d);
  192. counterTest(e);
  193. counterTest(f);
  194. return 0;
  195. }
  196.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
DAD2HAH3 2pairs
DAD3D8D9DJH5H8HKSASKCAC5C8CK 7pairs
SAS2S3S4S5S6S7S8S9C2C3C4C5C6C7C8C9CT 9pairs
D2H2S2C2DK 2pairs
HAH2H3H4H5H6H7H8H9HTHJHQHKCAC2C3C4C5C6C7C8C9CTCJCQCK 13pairs
HAH2C2CAS7SAD8DA 4pairs