forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker.cpp
140 lines (139 loc) · 2.25 KB
/
poker.cpp
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
131
132
133
134
135
136
137
138
139
// 2010-11-13
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
int main()
{
int T,i,j,k;
cin >> T;
map<char,int> rankrank;
map<char,int> suitsuit;
for (i=2; i<10; i++)
rankrank[i+'0']=i-2;
rankrank['T']=8;
rankrank['J']=9;
rankrank['Q']=10;
rankrank['K']=11;
rankrank['A']=12;
suitsuit['D']=0;
suitsuit['C']=1;
suitsuit['H']=2;
suitsuit['S']=3;
while (T--)
{
int rankcount[13]={0};
int suitcount[4]={0};
for (i=0; i<5; i++)
{
string s;
cin >> s;
rankcount[rankrank[s[0]]]++;
suitcount[suitsuit[s[1]]]++;
}
//royal flush?
bool b=false;
for (i=0; i<4; i++)
if (suitcount[i]==5)
{
bool ok=true;
for (j=rankrank['T']; j<=rankrank['A']; j++)
if (!rankcount[j]) ok=false;
if (ok) b=true;
}
if (b)
{
printf("royal flush\n");
continue;
}
//straight flush?
for (i=0; i<4; i++)
if (suitcount[i]==5)
for (j=0; j<=10; j++)
{
bool ok=true;
for (k=0; k<5; k++)
if (!rankcount[(j+k+rankrank['A'])%13])
ok=false;
if (ok) b=true;
}
if (b)
{
printf("straight flush\n");
continue;
}
//four of a kind?
for (i=0; i<13; i++)
if (rankcount[i]==4)
b=true;
if (b)
{
printf("four of a kind\n");
continue;
}
//full house?
for (i=0; i<13; i++)
for (j=0; j<13; j++)
if (rankcount[i]==3&&rankcount[j]==2)
b=true;
if (b)
{
printf("full house\n");
continue;
}
//flush?
for (i=0; i<4; i++)
if (suitcount[i]==5)
b=true;
if (b)
{
printf("flush\n");
continue;
}
//straight?
for (j=0; j<=10; j++)
{
bool ok=true;
for (k=0; k<5; k++)
if (!rankcount[(j+k+rankrank['A'])%13])
ok=false;
if (ok) b=true;
}
if (b)
{
printf("straight\n");
continue;
}
//3 of a kind?
for (i=0; i<13; i++)
if (rankcount[i]==3)
b=true;
if (b)
{
printf("three of a kind\n");
continue;
}
//2 pairs
for (i=0; i<13; i++)
for (j=0; j<i; j++)
if (rankcount[i]==2&&rankcount[j]==2)
b=true;
if (b)
{
printf("two pairs\n");
continue;
}
//pair
for (i=0; i<13; i++)
if (rankcount[i]==2)
b=true;
if (b)
printf("pair\n");
else
printf("high card\n");
}
return 0;
}