fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <bitset>
  5. #include <limits>
  6. #include <unordered_set>
  7. using namespace std;
  8.  
  9. const int number_of_suits = 4;
  10. const int number_of_rand_cards = 30;
  11. const int number_of_ranks = 13;
  12.  
  13. //types of card
  14. typedef enum suit_t {
  15. spade = 0,
  16. club,
  17. diamond,
  18. heart
  19. }suit_t;
  20.  
  21. //value each card holds
  22. typedef enum rank_t {
  23. ace = 0,
  24. two,
  25. three,
  26. four,
  27. five,
  28. six,
  29. seven,
  30. eight,
  31. nine,
  32. ten,
  33. jack,
  34. king,
  35. queen
  36. }rank_t;
  37.  
  38.  
  39. typedef struct poker_card {
  40. suit_t suit;
  41. rank_t rank;
  42. poker_card(suit_t suit_, rank_t rank_) : suit(suit_), rank(rank_) {}
  43. }poker_card;
  44.  
  45. class Poker {
  46. vector<poker_card> cards;
  47.  
  48. private:
  49. void inc_count(const bitset<number_of_ranks> &bs,
  50. int count[number_of_suits],
  51. suit_t suit,
  52. int index) {
  53. if (bs.test(index)) {
  54. count[suit]++;
  55. }
  56. else {
  57. count[suit] = 0;
  58. }
  59. }
  60.  
  61. bool detect_street(const bitset<number_of_ranks> &spade_bs,
  62. const bitset<number_of_ranks> &club_bs,
  63. const bitset<number_of_ranks> &diamond_bs,
  64. const bitset<number_of_ranks> &heart_bs) {
  65. int count[number_of_suits] = { 0 };
  66. int index = 0; // index starts at 0, bc. the rank 'ace' is assigned to value 0
  67.  
  68. // iterate number_of_ranks+1 times to cover case: ten, jack, king, queen, ace
  69. for (int i = 0; i <= number_of_ranks; i++) {
  70. inc_count(spade_bs, count, spade, index);
  71. inc_count(club_bs, count, club, index);
  72. inc_count(diamond_bs, count, diamond, index);
  73. inc_count(heart_bs, count, heart, index);
  74.  
  75. if (count[spade] == 5 ||
  76. count[club] == 5 ||
  77. count[diamond] == 5 ||
  78. count[heart] == 5) {
  79. return true;
  80. }
  81.  
  82. index++;
  83. index = index % number_of_ranks;
  84. }
  85.  
  86. return false;
  87. }
  88.  
  89. public:
  90. void add_card(const poker_card &c) {
  91. cards.push_back(c);
  92. }
  93.  
  94. bool detect_straight_flush() {
  95. // if cards size is less than 5, we cannot have a flush
  96. if (cards.size() < 5) {
  97. return false;
  98. }
  99.  
  100. // used space are 16 bytes -> O(1)
  101. bitset<number_of_ranks> spade_bs;
  102. bitset<number_of_ranks> club_bs;
  103. bitset<number_of_ranks> diamond_bs;
  104. bitset<number_of_ranks> heart_bs;
  105.  
  106. // O(n)
  107. for (int i = 0; i < cards.size(); i++) {
  108. switch (cards[i].suit) {
  109. case spade:
  110. spade_bs.set(cards[i].rank);
  111. break;
  112.  
  113. case club:
  114. club_bs.set(cards[i].rank);
  115. break;
  116.  
  117. case diamond:
  118. diamond_bs.set(cards[i].rank);
  119. break;
  120.  
  121. case heart:
  122. heart_bs.set(cards[i].rank);
  123. break;
  124.  
  125. default:
  126. break;
  127. }
  128. }
  129.  
  130. // check if one bitsize field is at least 5 cards, otherwize return false
  131. if (spade_bs.size() < 5 &&
  132. club_bs.size() < 5 &&
  133. diamond_bs.size() < 5 &&
  134. heart_bs.size() < 5) {
  135. return false;
  136. }
  137.  
  138. // O(1)
  139. return detect_street(spade_bs, club_bs, diamond_bs, heart_bs);
  140. }
  141. };
  142.  
  143. int main() {
  144. // your code goes here
  145.  
  146. Poker p1;
  147. Poker p2;
  148. Poker p3;
  149.  
  150. p1.add_card(poker_card(heart, ten));
  151. p1.add_card(poker_card(heart, king));
  152. p1.add_card(poker_card(heart, ace));
  153. p1.add_card(poker_card(heart, queen));
  154. p1.add_card(poker_card(heart, jack));
  155.  
  156. p2.add_card(poker_card(heart, two));
  157. p2.add_card(poker_card(heart, three));
  158. p2.add_card(poker_card(heart, ace));
  159. p2.add_card(poker_card(heart, four));
  160. p2.add_card(poker_card(heart, five));
  161.  
  162. p3.add_card(poker_card(heart, two));
  163. p3.add_card(poker_card(heart, three));
  164. p3.add_card(poker_card(heart, seven));
  165. p3.add_card(poker_card(heart, four));
  166. p3.add_card(poker_card(heart, five));
  167.  
  168. cout << p1.detect_straight_flush() << endl;
  169. cout << p2.detect_straight_flush() << endl;
  170. cout << p3.detect_straight_flush() << endl;
  171.  
  172. return 0;
  173. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1
1
0