uva10315,

## Problem:

Problem D
Poker Hands
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB

A poker deck contains 52 cards - each card has a suit which is one of clubs, diamonds, hearts, or spades (denoted C, D, H, and S in the input data). Each card also has a value which is one of 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A). For scoring purposes, the suits are unordered while the values are ordered as given above, with 2 being the lowest and ace the highest value.
A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest
• High Card: Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.
• Pair: 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.
• Two Pairs: The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.
• Three of a Kind: Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.
• Straight: Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.
• Flush: Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.
• Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
• Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.
• Straight flush: 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.
Your job is to compare several pairs of poker hands and to indicate which, if either, has a higher rank.
Input
The input file contains several lines, each containing the designation of 10 cards: the first 5 cards are the hand for the player named "Black" and the next 5 cards are the hand for the player named "White".

Output
For each line of input, print a line containing one of the following three lines:
` `
`   Black wins.`
`   White wins.`
`   Tie.`

Sample Input
` `
`2H 3D 5S 9C KD 2C 3H 4S 8C AH`
`2H 4S 4C 2D 4H 2S 8S AS QS 3S`
`2H 3D 5S 9C KD 2C 3H 4S 8C KH`
`2H 3D 5S 9C KD 2D 3H 5C 9S KH`

Sample Output
`White wins.`
`Black wins.`
`Black wins.`
`Tie.`

(The Decider Contest, Source: Waterloo ACM Programming Contest)

## Solution:

ad-hoc, but coding really takes a lot of time.

## Source Code:

//Mon Mar 14 01:48:48 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Card
{
public:
char suit;
int value;

Card(char v, char c)
{
this->convert(v);
this->suit = c;
}

//Convert the 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A to numbers;

void convert(char x)
{
switch (x)
{
case 'A':
value = 14;
return;
case 'K':
value = 13;
return;
case 'Q':
value = 12;
return;
case 'J':
value = 11;
return;
case 'T':
value = 10;
return;
default:
value = x - '0';
return;
}
}

static bool cmp(const Card &A, const Card &B)
{
return A.value < B.value;
}
};

enum HANDRANK
{
HIGH_CARD, PAIR, TWO_PAIR, THREE_KIND,
STRAIGHT, FLUSH, FULL_HOUSE, FOUR_KIND,
STRAIGHT_FLUSH
};

class Player
{
public:
vector<Card> card;
HANDRANK rank;
vector<int> values;

Player(string s)
{
//cout << "," << s << ',' << endl;
card.push_back(Card(s, s));
card.push_back(Card(s, s));
card.push_back(Card(s, s));
card.push_back(Card(s, s));
card.push_back(Card(s, s));

std::sort(card.begin(), card.end(), Card::cmp);
rank = getRank();
setValue();
}

bool isStraight()
{
for (int i = 0; i < 4; i++)
{
if (card[i].value + 1 != card[i + 1].value)
return false;
}
return true;
}

bool isFlush()
{
for (int i = 0; i < 4; i++)
if (card[i].suit != card[i + 1].suit)
return false;
return true;
}

bool isFourKind()
{
if (card.value == card.value && card.value == card.value && card.value == card.value)
return true;
if (card.value == card.value && card.value == card.value && card.value == card.value)
return true;
return false;
}

bool isThreeKind()
{
if (card.value == card.value)
return true;
if (card.value == card.value)
return true;
if (card.value == card.value)
return true;
return false;
}

int numOfPairs()
{
int count = 0;
for (int i = 0; i < 4; i++)
if (card[i].value == card[i + 1].value)
{
count++;
i++;
}
return count;
}

HANDRANK getRank()
{
bool flag1 = isStraight();
bool flag2 = isFlush();
bool flag3 = isFourKind();
bool flag4 = isThreeKind();
int pair = numOfPairs();
//cout << pair << endl;
if (flag1 && flag2)
return STRAIGHT_FLUSH;
if (flag3)
return FOUR_KIND;
//Take care here, there should be 2 fake pairs here;
//pair should not be 1;
if (flag4 && pair == 2)
return FULL_HOUSE;
if (flag2)
return FLUSH;
if (flag1)
return STRAIGHT;
if (flag4)
return THREE_KIND;
if (pair == 2)
return TWO_PAIR;
if (pair == 1)
return PAIR;
return HIGH_CARD;
}

void setValue()
{
for (int i = 0; i < 5; i++)
values.push_back(0);
switch (rank)
{
case STRAIGHT_FLUSH:
case STRAIGHT:
values = card.value;
break;
case FLUSH:
case HIGH_CARD:
for (int i = 0; i < 5; ++i)
values[i] = card[4 - i].value;
break;
case FULL_HOUSE:
case FOUR_KIND:
case THREE_KIND:
values = card.value;
break;
case TWO_PAIR:
//Only three position for the single card: 0, 2, 4.
if (card.value != card.value)
{ //ex. 2 33 44
values = card.value;
values = card.value;
values = card.value;
}
else if (card.value != card.value)
{ //ex.33 4 55
values = card.value;
values = card.value;
values = card.value;
}
else
{ //ex.55 66 7
values = card.value;
values = card.value;
values = card.value;
}
break;
case PAIR:
int pairValue;
for (int i = 0; i <= 3; ++i) //search pair
if (card[i].value == card[i + 1].value)
{
pairValue = card[i].value;
break;
}
values = pairValue;
for (int i = 4, r = 1; i >= 0; --i)
{ //i for card, r for values
if (card[i].value == pairValue)
continue;
values[r++] = card[i].value;
}
break;
}
}

int compareTo(Player other)
{
if (rank > other.rank)
return 1;
if (rank < other.rank)
return -1;
for (int i = 0; i < 5; i++)
{
if (values[i] > other.values[i])
return 1;
if (values[i] < other.values[i])
return -1;
}
return 0;
}
};

int main(int argc, char* argv[])
{
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
string str;
while (getline(cin, str))
{
Player black(str.substr(0, 14));
Player white(str.substr(15));
switch (black.compareTo(white))
{
case 1:
cout << "Black wins." << endl;
break;
case -1:
cout << "White wins." << endl;
break;
case 0:
cout << "Tie." << endl;
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}