Texas hold’em Poker
Code
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
#define LL long long
#define ULL unsigned long long
#define debug(a) cout << #a << " " << a << endl
#define de(a) cout << a << " "
#define fi first
#define se second
const int maxn = 150;
const int N = 1e5 + 7, M = N * 2;
const int inf = 0x3f3f3f3f;
const LL INF = 0xFFFFFFFFFF;
const long long mod = 1e9 + 7;
inline long long read();
struct Node {
string name;
int card[maxn];
int level = -1;
int tag = -1;
int tag2 = -1;
int sum = 0;
bool operator<(const Node &w) const {
if (level == w.level) {
if (tag == w.tag) {
if (tag2 == w.tag2) {
if (sum == w.sum) {
return name > w.name;
}
return sum < w.sum;
}
return tag2 < w.tag2;
}
return tag < w.tag;
}
return level < w.level;
}
} node[N];
int n;
bool RoyalStraight(int a[]) {
return a[1] == 1 && a[2] == 10 && a[3] == 11 && a[4] == 12 && a[5] == 13;
}
bool Straight(int a[]) {
bool flag = true;
for (int i = 2; i <= 5; i++) {
if (a[i] - a[i - 1] != 1) {
flag = false;
break;
}
}
return flag;
}
bool FourOfAKind(int a[]) {
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[a[i]]++;
}
for (auto x:mp) {
if (x.second == 4) return true;
}
return false;
}
bool FullHouse(int a[]) {
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[a[i]]++;
}
int cnt = 0;
for (auto x:mp) {
if (x.second == 3) cnt++;
if (x.second == 2) cnt++;
}
if (cnt == 2 && mp.size() == 2) return true;
else return false;
}
bool ThreeOfAKind(int a[]) {
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[a[i]]++;
}
for (auto x:mp) {
if (x.second == 3) return true;
}
return false;
}
bool TwoPairs(int a[]) {
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[a[i]]++;
}
int cnt = 0;
for (auto x:mp) {
if (x.second == 2) cnt++;
}
if (cnt == 2) return true;
else return false;
}
bool Pair(int a[]) {
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[a[i]]++;
}
int cnt = 0;
for (auto x:mp) {
if (x.second == 2) return true;
}
return false;
}
void judge(Node &x) {
int lv = -1, sum = 0;
if (RoyalStraight(x.card)) {
x.level = 8;
}
else if (Straight(x.card)) {
x.level = 7;
x.tag = x.card[5];
}
else if (FourOfAKind(x.card)) {
x.level = 6;
if (x.card[1] == x.card[2]) x.tag = x.card[1], x.tag2 = x.card[5];
else x.tag = x.card[5], x.tag2 = x.card[1];
}
else if (FullHouse(x.card)) {
x.level = 5;
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[x.card[i]]++;
}
int cnt = 0;
for (auto idx:mp) {
if (idx.second == 3) x.tag = idx.first;
else if (idx.second == 2) x.sum = idx.first;
}
}
else if (ThreeOfAKind(x.card)) {
x.level = 4;
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[x.card[i]]++;
}
int cnt = 0;
for (auto idx:mp) {
if (idx.second == 3) x.tag = idx.first;
if (idx.second == 1) x.sum += idx.first;
}
}
else if (TwoPairs(x.card)) {
x.level = 3;
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[x.card[i]]++;
}
int cnt = 0;
x.tag2 = inf;
for (auto idx:mp) {
if (idx.second == 2) x.tag = max(x.tag, idx.first), x.tag2 = min(x.tag2, idx.first);
if (idx.second == 1) x.sum += idx.first;
}
}
else if (Pair(x.card)) {
x.level = 2;
map<int, int> mp;
for (int i = 1; i <= 5; i++) {
mp[x.card[i]]++;
}
int cnt = 0;
for (auto idx:mp) {
if (idx.second == 2) x.tag = idx.first;
if (idx.second == 1) x.sum += idx.first;
}
}
else {
x.level = 1;
for (int i = 1; i <= 5; i++) x.sum += x.card[i];
}
}
bool cmp(int a, int b) {
return a < b;
}
void solve() {
for (int i = 1; i <= n; i++) {
node[i].level = -1, node[i].tag = -1, node[i].tag2 = -1, node[i].sum = 0;
char cards[maxn];
string name;
cin >> name;
scanf("%s", cards);
node[i].name = name;
int pos = 1;
for (int j = 0; j < strlen(cards); j++) {
bool ten = false;
if (cards[j] == 'A') node[i].card[pos] = 1;
else if (cards[j] == '1' && j < strlen(cards) - 1 && cards[j + 1] == '0')
node[i].card[pos] = 10, ten = true;
else if (cards[j] == 'J') node[i].card[pos] = 11;
else if (cards[j] == 'Q') node[i].card[pos] = 12;
else if (cards[j] == 'K') node[i].card[pos] = 13;
else {
node[i].card[pos] = cards[j] - '0';
}
if (ten) j++;
pos++;
}
sort(node[i].card + 1, node[i].card + 1 + 5, cmp);
judge(node[i]);
}
sort(node + 1, node + 1 + n);
for (int i = n; i >= 1; i--) {
cout << node[i].name << '\n';
}
}
int main() {
while (~scanf("%d", &n)) {
solve();
}
return 0;
}
inline LL read() {
char ch = getchar();
LL p = 1, data = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')p = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
data = data * 10 + (ch ^ 48);
ch = getchar();
}
return p * data;
}
Test Case
22
1.1 10JQKA
1.2 10JQKA
2.1 34567
2.2 23456
3.1 55553
3.2 55552
3.3 44443
3.4 44442
4.1 44422
4.2 33322
4.3 333AA
5.1 333JK
5.2 33312
5.3 222JK
6.1 KKQQ1
6.2 KKJJ3
6.3 QQJJ2
7.1 KK234
7.2 KKA23
7.3 QQKJ10
8.1 246810
8.2 A3579