Source code for submission s994

rhino.cpp

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <complex>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <list>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <sstream>
  14. #include <stack>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18.  
  19. using namespace std;
  20. #define DEBUG(x) cerr << ">>> " << #x << " : " << x << endl;
  21. #define REP(i,a) for (int i = 0; i < (a); ++i)
  22. #define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
  23. #define FORD(i,a,b) for (int i = (a); i >= (b); --i)
  24. inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
  25.  
  26. const int INF = 1<<29;
  27. typedef long long ll;
  28. //////////////////////////////////
  29. typedef vector<pair<int, int> > ruka;
  30.  
  31. pair<int, int> convert(char c[]){
  32. pair<int, int> ans;
  33. ans.first = c[0] - '0';
  34. if(c[0] == 'X') ans.first = 10;
  35. if(c[0] == 'J') ans.first = 11;
  36. if(c[0] == 'Q') ans.first = 12;
  37. if(c[0] == 'K') ans.first = 13;
  38. if(c[0] == 'A') ans.first = 14;
  39. ans.second = c[1];
  40. return ans;
  41. }
  42.  
  43. bool One(ruka &r){
  44. sort(r.begin(), r.end(), greater<pair<int, int> >());
  45. return true;
  46. }
  47.  
  48. bool Flush(ruka &r){
  49. REP(i, 5) if(r[i].second != r[0].second) return false;
  50. One(r);
  51. return true;
  52. }
  53.  
  54. bool FullHouse(ruka &r){
  55. One(r);
  56. if(r[0].first == r[1].first && r[1].first == r[2].first && r[3].first == r[4].first) return true;
  57. reverse(r.begin(), r.end());
  58. if(r[0].first == r[1].first && r[1].first == r[2].first && r[3].first == r[4].first) return true;
  59. return false;
  60. }
  61.  
  62. bool Straight(ruka &r){
  63. One(r);
  64. if(r[0].first == 14 && r[1].first == 5 && r[2].first == 4 && r[3].first == 3 && r[4].first == 2){
  65. r[0].first = 0;
  66. One(r);
  67. r[4].first = 14;
  68. return true;
  69. }
  70. REP(i, 4) if(r[i+1].first != r[i].first + 1) return false;
  71. return true;
  72. }
  73.  
  74. bool RoyalFlush(ruka &r){ return Flush(r) && Straight(r); }
  75.  
  76. bool Poker(ruka &r){
  77. One(r);
  78. bool ok = true;
  79. REP(i, 4) if(r[i].first != r[0].first){ ok = false; break; }
  80. if(ok) return true;
  81. reverse(r.begin(), r.end());
  82. ok = true;
  83. REP(i, 4) if(r[i].first != r[0].first){ ok = false; break; }
  84. if(ok) return true;
  85. return false;
  86. }
  87.  
  88. bool Three(ruka &r){
  89. One(r);
  90. if(r[0].first == r[1].first && r[1].first == r[2].first) return true;
  91. if(r[3].first == r[1].first && r[1].first == r[2].first){ swap(r[0], r[3]); return true; }
  92. if(r[3].first == r[4].first && r[4].first == r[2].first){ swap(r[0], r[3]); swap(r[1], r[4]); return true; }
  93. return false;
  94. }
  95.  
  96. bool Pair(ruka &r){
  97. One(r);
  98. if(r[0].first == r[1].first) return true;
  99. if(r[2].first == r[1].first){ swap(r[0], r[2]); return true; }
  100. if(r[3].first == r[2].first){ swap(r[0], r[2]); swap(r[1], r[3]); return true; }
  101. if(r[3].first == r[4].first){ swap(r[0], r[3]); swap(r[1], r[4]); sort(r.begin() + 2, r.end(), greater<pair<int, int> >()); return true; }
  102. return false;
  103. }
  104.  
  105. bool TwoPairs(ruka &r){
  106. One(r);
  107. if(r[0].first == r[1].first && r[2].first == r[3].first) return true;
  108. if(r[0].first == r[1].first && r[3].first == r[4].first){ swap(r[2], r[4]); return true; }
  109. if(r[1].first == r[2].first && r[3].first == r[4].first){ swap(r[0], r[2]); swap(r[2], r[4]); return true; }
  110. return false;
  111. }
  112.  
  113. int eval(ruka &a){
  114. if(RoyalFlush(a)) return 8;
  115. if(Poker(a)) return 7;
  116. if(FullHouse(a)) return 6;
  117. if(Flush(a)) return 5;
  118. if(Straight(a)) return 4;
  119. if(Three(a)) return 3;
  120. if(TwoPairs(a)) return 2;
  121. if(Pair(a)) return 1;
  122. One(a);
  123. return 0;
  124. }
  125.  
  126. bool cmp(ruka &a, ruka &b){
  127. int vA = eval(a), vB = eval(b);
  128. if(vA > vB) return true;
  129. if(vB > vA) return false;
  130. REP(i, 5){
  131. if(a[i].first > b[i].first) return true;
  132. if(a[i].first < b[i].first) return false;
  133. }
  134. return true;
  135. }
  136.  
  137. ruka stul;
  138. ruka hraci[111];
  139. ruka nejlepsi;
  140. vector<int> odpoved;
  141. int N;
  142. int main() {
  143. while(scanf("%d", &N) != EOF){
  144. char tmp[5];
  145. stul.clear();
  146. odpoved.clear();
  147. REP(i, 5){ scanf("%s", tmp); stul.push_back(convert(tmp)); }
  148. REP(i, N){
  149. hraci[i].clear();
  150. REP(j, 2){ scanf("%s", tmp); hraci[i].push_back(convert(tmp)); }
  151. ruka best = stul;
  152. REP(j, 5){
  153. ruka tmp = stul;
  154. tmp[j] = hraci[i][0];
  155. if(cmp(tmp, best)) best = tmp;
  156. }
  157. REP(j, 5){
  158. ruka tmp = stul;
  159. tmp[j] = hraci[i][1];
  160. if(cmp(tmp, best)) best = tmp;
  161. }
  162. REP(j, 5) REP(k, j){
  163. ruka tmp = stul;
  164. tmp[j] = hraci[i][0];
  165. tmp[k] = hraci[i][1];
  166. if(cmp(tmp, best)) best = tmp;
  167. }
  168. if(i == 0 || cmp(best, nejlepsi)){ if(i && !cmp(nejlepsi, best)) odpoved.clear(); odpoved.push_back(i); nejlepsi = best; }
  169. }
  170. REP(i, odpoved.size()){
  171. if(i) printf(" ");
  172. printf("%d", odpoved[i] + 1);
  173. }
  174. printf("\n");
  175. }
  176. return 0;
  177. }
  178.