-
Notifications
You must be signed in to change notification settings - Fork 0
/
PE054.hpp
130 lines (129 loc) · 4.33 KB
/
PE054.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
using namespace std;
int PE054() {
freopen("PE054.txt", "r", stdin);
unordered_map<char, int> val = {{'2', 1}, {'3', 2}, {'4', 3}, {'5', 4}, {'6', 5}, {'7', 6}, {'8', 7}, {'9', 8}, {'T', 9}, {'J', 10}, {'Q', 11}, {'K', 12}, {'A', 13}},
suit = {{'S', 1}, {'C', 2}, {'D', 3}, {'H', 4}};
int res = 0;
string s;
function<bool(vector<pair<int, int>>)> isHighCard = [] (vector<pair<int, int>> hand) {
return true;
};
function<bool(vector<pair<int, int>>)> isOnePair = [] (vector<pair<int, int>> hand) {
sort(begin(hand), end(hand));
int cnt = 0;
for (int i = 1; i < 5; i ++) {
cnt += hand[i].first == hand[i - 1].first;
}
return cnt == 1;
};
function<bool(vector<pair<int, int>>)> isTwoPairs = [] (vector<pair<int, int>> hand) {
sort(begin(hand), end(hand));
return (hand[0].first == hand[1].first && (hand[2].first == hand[3].first || hand[3].first == hand[4].first))
|| (hand[1].first == hand[2].first && hand[3].first == hand[4].first);
};
function<bool(vector<pair<int, int>>)> isThreeOfAKind = [&] (vector<pair<int, int>> hand) {
sort(begin(hand), end(hand));
return hand[0].first == hand[2].first || hand[1].first == hand[3].first || hand[2].first == hand[4].first;
};
function<bool(vector<pair<int, int>>)> isStraight = [&] (vector<pair<int, int>> hand) {
sort(begin(hand), end(hand));
for (int i = 1; i < 5; i ++) {
if (hand[i].first != hand[0].first + i) {
if (i == 4 && hand[i].first - 12 == hand[0].first) {
continue;
}
return false;
}
}
return true;
};
function<bool(vector<pair<int, int>>)> isFlush = [&] (vector<pair<int, int>> hand) {
for (int i = 1; i < 5; i ++) {
if (hand[i].second != hand[0].second) {
return false;
}
}
return true;
};
function<bool(vector<pair<int, int>>)> isFullHouse = [&] (vector<pair<int, int>> hand) {
return isThreeOfAKind(hand) && isOnePair(hand);
};
function<bool(vector<pair<int, int>>)> isFourOfAKind = [] (vector<pair<int, int>> hand) {
sort(begin(hand), end(hand));
return hand[0].first == hand[3].first || hand[1].first == hand[4].first;
};
function<bool(vector<pair<int, int>>)> isStraightFlush = [&] (vector<pair<int, int>> hand) {
return isStraight(hand) && isFlush(hand);
};
function<bool(vector<pair<int, int>>, vector<pair<int, int>>)> greaterHighCard = [&] (vector<pair<int, int>> p1, vector<pair<int, int>> p2) {
sort(begin(p1), end(p1), greater<pair<int, int>>());
sort(begin(p2), end(p2), greater<pair<int, int>>());
int len = p1.size();
for (int i = 0; i < len; i ++) {
if (p1[i].first > p2[i].first) {
return true;
} else if (p1[i].first < p2[i].first) {
return false;
}
}
return false;
};
function<bool(vector<pair<int, int>>, vector<pair<int, int>>)> greaterOnePair = [&] (vector<pair<int, int>> p1, vector<pair<int, int>> p2) {
sort(begin(p1), end(p1), greater<pair<int, int>>());
sort(begin(p2), end(p2), greater<pair<int, int>>());
vector<pair<int, int>> high1, high2;
int pair1, pair2;
for (int i = 0; i < 5; i ++) {
if (high1.size() && p1[i].first == high1.back().first) {
high1.pop_back();
pair1 = p1[i].first;
} else {
high1.push_back({p1[i].first, 1});
}
if (high2.size() && p2[i].first == high2.back().first) {
high2.pop_back();
pair2 = p2[i].first;
} else {
high2.push_back({p2[i].first, 1});
}
}
if (pair1 > pair2) {
return true;
} else if (pair1 == pair2) {
return greaterHighCard(high1, high2);
}
return false;
};
while (cin >> s) {
vector<pair<int, int>> p1 = {{val[s[0]], suit[s[1]]}}, p2;
for (int i = 1; i < 5; i ++) {
cin >> s;
p1.push_back({val[s[0]], suit[s[1]]});
}
for (int i = 0; i < 5; i ++) {
cin >> s;
p2.push_back({val[s[0]], suit[s[1]]});
}
vector<bool> L = {isStraightFlush(p1), isFourOfAKind(p1), isFullHouse(p1), isFlush(p1), isStraight(p1), isThreeOfAKind(p1), isTwoPairs(p1), isOnePair(p1), isHighCard(p1)},
R = {isStraightFlush(p2), isFourOfAKind(p2), isFullHouse(p2), isFlush(p2), isStraight(p2), isThreeOfAKind(p2), isTwoPairs(p2), isOnePair(p2), isHighCard(p2)};
for (int i = 0; i < 9; i ++) {
if (L[i] && R[i]) {
switch (i) {
case 7:
res += greaterOnePair(p1, p2);
break;
case 8:
res += greaterHighCard(p1, p2);
break;
}
break;
} else if (L[i] || R[i]) {
res += L[i];
break;
}
}
}
cin.clear();
return res;
}