/*;
* Allen Cheng
* Thomas Jefferson High School for Science and
Technology
* ACSL Contest 4, Junior Division 2012-2013
* ACSL RUMMY
*/
import
java.util.*;
import java.io.*;
public class
ACSLRummy_Cheng {
private static final int LINES = 5;
private static final int LENGTH = 7;
public static void main(String[] args)
throws IOException {
BufferedReader read = new
BufferedReader(new InputStreamReader(
System.in));
for (int m = 0; m < LINES; m++) {
try {
System.out
.println("Please
enter rummy hand with a comma and space between each card:");
StringTokenizer st = new
StringTokenizer(read.readLine(), ", ");
Card[] hand = new
Card[LENGTH];
for (int i = 0; i <
LENGTH; i++)
hand[i] = new Card(st.nextToken());
int[] nums = new
int[13];
int[] suits = new
int[4];
for (int i = 0; i <
LENGTH; i++) {
nums[hand[i].num -
1]++;
suits[hand[i].suit]++;
}
Card[] sorted = new
Card[LENGTH];
boolean hasFour = false;
boolean done = false;
for (int i = 0; i <
suits.length; i++) {
if (suits[i] >
3) {
Card[] all =
new Card[suits[i]];
Card[] run =
new Card[4];
int ind = 0;
for (int j =
0; j < LENGTH; j++) {
if (hand[j].suit
== i) {
all[ind]
= hand[j];
ind++;
}
}
Arrays.sort(all);
for (int j =
0; j < all.length - 3; j++) {
if
(all[j].num == all[j + 1].num - 1
&&
all[j].num == all[j + 2].num - 2
&& all[j].num == all[j
+ 3].num - 3) {
for
(int k = 0; k < run.length; k++)
run[k]
= all[j + k];
hasFour
= true;
break;
}
}
if (run[0]
== null)
continue;
for (int j =
0; j < LENGTH; j++) {
if
(hand[j].suit == i && hand[j].num >= run[0].num
&&
hand[j].num <= run[run.length - 1].num) {
nums[hand[j].num
- 1]--;
suits[hand[j].suit]--;
hand[j]
= null;
}
}
for (int j =
0; j < run.length; j++)
sorted[j]
= run[j];
break;
}
}
for (int i = 0; i <
nums.length && !hasFour; i++) {
if (nums[i] >
3) {
hasFour =
true;
Card[] set =
new Card[4];
int ind = 0;
for (int j =
0; j < LENGTH; j++) {
if
(hand[j] != null && hand[j].num == i + 1) {
set[ind]
= hand[j];
nums[hand[j].num
- 1]--;
suits[hand[j].suit]--;
hand[j]
= null;
ind++;
if
(ind > 3)
break;
}
}
Arrays.sort(set);
for (int j =
0; j < set.length; j++)
sorted[j]
= set[j];
}
}
Card[] temp = new
Card[3];
for (int i = 0; i <
suits.length; i++) {
if (suits[i] >
2) {
Card[] all =
new Card[suits[i]];
Card[] run =
new Card[3];
int ind = 0;
for (int j =
0; j < LENGTH; j++) {
if
(hand[j] != null && hand[j].suit == i) {
all[ind]
= hand[j];
ind++;
}
}
Arrays.sort(all);
for (int j = 0; j < all.length - 2; j++) {
if
(all[j].num == all[j + 1].num - 1
&&
all[j].num == all[j + 2].num - 2) {
for
(int k = 0; k < run.length; k++)
run[k]
= all[j + k];
}
}
if (run[0]
== null)
continue;
for (int j =
0; j < LENGTH; j++) {
if
(hand[j] != null && hand[j].suit == i
&&
hand[j].num >= run[0].num
&&
hand[j].num <= run[run.length - 1].num) {
nums[hand[j].num
- 1]--;
suits[hand[j].suit]--;
hand[j]
= null;
}
}
if (hasFour)
{
for
(int j = 1; j <= run.length; j++)
sorted[LENGTH
- j] = run[run.length - j];
done =
true;
break;
} else {
if
(temp[0] != null) {
if
(temp[0].compareTo(run[0]) < 0) {
for
(int j = 0; j < temp.length; j++)
sorted[0]
= temp[0];
for
(int j = 0; j < run.length; j++)
sorted[j
+ temp.length] = run[j];
}
else {
for
(int j = 0; j < run.length; j++)
sorted[0]
= run[0];
for
(int j = 0; j < temp.length; j++)
sorted[j
+ run.length] = temp[j];
}
break;
}
for
(int j = 0; j < run.length; j++)
temp[j]
= run[j];
}
}
}
if (done) {
print(sorted);
continue;
}
if (temp[0] != null) {
for (int i = 0; i
< temp.length; i++) {
sorted[i] =
temp[i];
}
}
temp = new Card[3];
for (int i = 0; i <
nums.length; i++) {
if (nums[i] >
2) {
Card[] set =
new Card[3];
int ind = 0;
for (int j =
0; j < LENGTH; j++) {
if
(hand[j] != null && hand[j].num - 1 == i) {
set[ind]
= hand[j];
nums[hand[j].num
- 1]--;
suits[hand[j].suit]--;
hand[j]
= null;
ind++;
if
(ind > 2)
break;
}
}
Arrays.sort(set);
if (hasFour)
{
for
(int j = 1; j <= set.length; j++)
sorted[LENGTH
- j] = set[set.length - j];
done =
true;
break;
} else {
if
(sorted[0] != null) {
for
(int j = 0; j < set.length; j++)
sorted[set.length
+ j] = set[j];
break;
}
if
(temp[0] != null) {
if
(temp[0].compareTo(set[0]) < 0) {
for
(int j = 0; j < temp.length; j++)
sorted[0]
= temp[0];
for
(int j = 0; j < set.length; j++)
sorted[j
+ temp.length] = set[j];
}
else {
for
(int j = 0; j < set.length; j++)
sorted[0]
= set[0];
for
(int j = 0; j < temp.length; j++)
sorted[j
+ set.length] = temp[j];
}
break;
}
for
(int j = 0; j < set.length; j++)
temp[j]
= set[j];
}
}
}
if (done) {
print(sorted);
continue;
}
if (temp[0] != null) {
for (int i = 0; i
< temp.length; i++) {
sorted[i] =
temp[i];
}
}
int count = 0;
for (int i = 0; i <
hand.length; i++) {
if (hand[i] !=
null)
count++;
}
Card[] other = new
Card[count];
int ind = 0;
for (int i = 0; i <
hand.length; i++) {
if (hand[i] !=
null) {
other[ind] =
hand[i];
ind++;
}
}
Arrays.sort(other);
for (int i = 0; i <
other.length; i++) {
if (i <
other.length - 1
&&
other[i].num == other[i + 1].num) {
sorted[sorted.length
- i - 2] = other[i];
sorted[sorted.length
- i - 1] = other[i + 1];
i++;
} else
sorted[sorted.length
- i - 1] = other[i];
}
print(sorted);
} catch (Throwable e) {
System.out
.println("Sorry,
this program cannot handle that input!\n");
}
}
}
private static void print(Card[] hand) {
System.out.print(hand[0]);
for (int i = 1; i < hand.length;
i++)
System.out.print(",
" + hand[i]);
System.out.println("\n");
}
}
class Card
implements Comparable<Card> {
int num;
int suit;
public Card(String s) {
char c = s.charAt(0);
switch (c) {
case 'T':
num = 10;
break;
case 'J':
num = 11;
break;
case 'Q':
num = 12;
break;
case 'K':
num = 13;
break;
case 'A':
num = 1;
break;
default:
num =
Integer.parseInt("" + c);
break;
}
c = s.charAt(1);
switch (c) {
case 'S':
suit = 0;
break;
case 'H':
suit = 1;
break;
case 'C':
suit = 2;
break;
default:
suit = 3;
break;
}
}
public int compareTo(Card other) {
if (other.num != num)
return num - other.num;
return suit - other.suit;
}
public String toString() {
String s = "";
switch (num) {
case 1:
s += "A";
break;
case 10:
s += "T";
break;
case 11:
s += "J";
break;
case 12:
s += "Q";
break;
case 13:
s += "K";
break;
default:
s += num;
break;
}
switch (suit) {
case 0:
s += "S";
break;
case 1:
s += "H";
break;
case 2:
s += "C";
break;
default:
s += "D";
}
return s;
}
}