/* 題目: UVa 10315 - Poker Hands
*
Language: C++
*
Created on: 2015年12月2日
*
Author: hanting
*/
// 2 3 4 5 6 > 1 2 3 4 5
#include <iostream>
#include <vector>
#include <algorithm> // sort
using namespace std;
int trans(char c)
{
if(isdigit(c)) return c - 48;
else
{
if(c == 'T') return 10;
else if(c == 'J') return 11;
else if(c == 'Q') return 12;
else if(c == 'K') return 13;
else if(c == 'A') return 14; // * A 不是1
}
}
bool input(vector<pair<int, int> > &black, vector<pair<int, int> > &white)
{
black.clear();
white.clear();
string str;
bool read = false;
for (int i = 0; i < 5; i++)
{
read = (cin >> str);
int num = trans(str[0]);
black.push_back(pair<int, int>(num, str[1]));
}
for (int i = 0; i < 5; i++)
{
cin
>> str;
int num = trans(str[0]);
white.push_back(pair<int, int>(num, str[1]));
}
return read;
}
int StraightFlush(vector<pair<int, int> > &vec)
{
int cnt = 0;
int maxi = 0;
for(int i = 1; i < vec.size(); i++)
{
if(vec[i].first == vec[i-1].first + 1 and vec[i].second == vec[i-1].second) cnt++, maxi = vec[i].first;
}
if (cnt == 4) return maxi;
return 0;
}
int FourOfAKind(vector<pair<int, int> > &vec)
{
int cnt = 0;
for(int i = 1; i < vec.size() and cnt != 3; i++)
{
if(vec[i].first == vec[i-1].first) cnt++;
else cnt = 0;
}
if (cnt == 3) return vec[3].first;
return 0;
}
int FullHouse(vector<pair<int, int> > &vec)
{
int cnt[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
cnt[tmp]++;
}
int three = -1, two = -1;
for(int i = 2; i < 15; i++)
{
if(cnt[i] == 3)
{
three = i;
}
else if(cnt[i] == 2)
{
two = i;
}
}
if(three != -1 and two != -1)
{
return three;
}
return 0;
}
int Flush(vector<pair<int, int> > &vec)
{
int cnt = 0;
int maxi = 0;
int flag[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
flag[tmp]++;
}
for(int i = 1; i < vec.size(); i++)
{
if(vec[i].second == vec[i-1].second) cnt++;
}
if(cnt == 4)
{
int sum = 0;
int j = 1;
for(int i = 2; i < 15; i++)
{
if(flag[i])
{
sum += i * j;
j *= 14;
}
}
return sum;
}
return 0;
}
int Straight(vector<pair<int, int> > &vec)
{
int cnt = 0;
int maxi = 0;
for(int i = 1; i < vec.size() and cnt != 4; i++)
{
if(vec[i].first == vec[i-1].first + 1)
{
cnt++;
maxi = vec[i].first;
}
else cnt = 0;
}
if(cnt == 4)
{
return maxi;
}
return 0;
}
int ThreeOfAKind(vector<pair<int, int> > &vec)
{
int cnt[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
cnt[tmp]++;
}
int j = 1;
int sum = 0;
int Three = 0;
for(int i = 2; i <= 14; i++)
{
if(cnt[i] == 3)
{
Three = 1;
sum += i*14*14*14;
}
else if(cnt[i])
{
sum += j * i;
j *= 14;
}
}
if(Three)
return sum;
return 0;
}
int TwoPairs(vector<pair<int, int> > &vec)
{
int cnt[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
cnt[tmp]++;
}
vector<int> two;
vector<int> one;
for(int i = 2; i < 15; i++)
{
if(cnt[i] == 2) two.push_back(i);
else if(cnt[i]) one.push_back(i);
}
if(two.size() == 2)
{
int big = max(two[0], two[1]);
int small = min(two[0], two[1]);
return (big * 10000 + small * 100 + one[0]);
}
return 0;
}
int Pair(vector<pair<int, int> > &vec)
{
int cnt[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
cnt[tmp]++;
}
vector<int> two;
vector<int> one;
for(int i = 2; i < 15; i++)
{
if(cnt[i] == 2) two.push_back(i);
else if(cnt[i]) one.push_back(i);
}
if(two.size() == 1)
{
return (two[0] * 10000 + one[2]*14*14 + one[1]*14 + one[0]);
}
return 0;
}
int HighCard(vector<pair<int, int> > &vec)
{
int cnt[15] = {0};
for(int i = 0; i < vec.size(); i++)
{
int tmp = vec[i].first;
cnt[tmp]++;
}
int j = 1;
int sum = 0;
for(int i = 2; i <= 14; i++)
{
if(cnt[i])
{
sum += i * j;
j *= 14;
}
}
return sum;
}
int compare(vector<pair<int, int> > &black, vector<pair<int, int> > &white)
{
int b, w;
b = StraightFlush(black);
w = StraightFlush(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = FourOfAKind(black);
w = FourOfAKind(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = FullHouse(black);
w = FullHouse(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = Flush(black);
w = Flush(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = Straight(black);
w = Straight(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = ThreeOfAKind(black);
w = ThreeOfAKind(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = TwoPairs(black);
w = TwoPairs(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = Pair(black);
w = Pair(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
b = HighCard(black);
w = HighCard(white);
if(b > w) return 0;
else if(b < w) return 1;
else if(b and b == w)
{
return 2;
}
}
int main()
{
vector<pair<int, int> > black, white;
while (input(black, white))
{
sort(black.begin(), black.end());
sort(white.begin(), white.end());
int ans = compare(black, white);
if(ans == 0)
{
cout << "Black wins." << endl;
}
else if(ans == 1)
{
cout << "White wins." << endl;
}
else
{
cout << "Tie." << endl;
}
}
return 0;
}