Problem:
Problem Statement

Texas hold 'em is one of the standard poker games, originated in Texas, United States.
It is played with a standard deck of 52 cards, which has 4 suits (spades, hearts, diamonds and clubs) and 13 ranks (A, K, Q, J and 10-2), without jokers.

With betting aside, a game goes as described below.

At the beginning each player is dealt with two cards face down.
These cards are called hole cards or pocket cards, and do not have to be revealed until the showdown.
Then the dealer deals three cards face up as community cards, i.e. cards shared by all players.
These three cards are called the flop.
The flop is followed by another community card called the turn then one more community card called the river.

After the river, the game goes on to the showdown.
All players reveal their hole cards at this point.
Then each player chooses five out of the seven cards, i.e. their two hole cards and the five community cards, to form a hand.
The player forming the strongest hand wins the game.
There are ten possible kinds of hands, listed from the strongest to the weakest:

Royal straight flush: A, K, Q, J and 10 of the same suit. This is a special case of straight flush.

Straight flush: Five cards in sequence (e.g. 7, 6, 5, 4 and 3) and of the same suit.

Four of a kind: Four cards of the same rank.

Full house: Three cards of the same rank, plus a pair of another rank.

Flush: Five cards of the same suit, but not in sequence.

Straight: Five cards in sequence, but not of the same suit.

Three of a kind: Just three cards of the same rank.

Two pairs: Two cards of the same rank, and two other cards of another same rank.

One pair: Just a pair of cards (two cards) of the same rank.

High card: Any other hand.

For the purpose of a sequence, J, Q and K are treated as 11, 12 and 13 respectively.
A can be seen as a rank either next above K or next below 2, thus both A-K-Q-J-10 and 5-4-3-2-A are possible (but not 3-2-A-K-Q or the likes).

If more than one player has the same kind of hand, ties are broken by comparing the ranks of the cards.
The basic idea is to compare first those forming sets (pairs, triples or quads) then the rest cards one by one from the highest-ranked to the lowest-ranked, until ties are broken.
More specifically:

Royal straight flush: (ties are not broken)

Straight flush: Compare the highest-ranked card.

Four of a kind: Compare the four cards, then the remaining one.

Full house: Compare the three cards, then the pair.

Flush: Compare all cards one by one.

Straight: Compare the highest-ranked card.

Three of a kind: Compare the three cards, then the remaining two.

Two pairs: Compare the higher-ranked pair, then the lower-ranked, then the last one.

One pair: Compare the pair, then the remaining three.

High card: Compare all cards one by one.

The order of the ranks is A, K, Q, J, 10, 9, ..., 2, from the highest to the lowest,
except for A next to 2 in a straight regarded as lower than 2.
Note that there are exceptional cases where ties remain.
Also note that the suits are not considered at all in tie-breaking.

Here are a few examples of comparison (note these are only intended for explanatory purpose; some combinations cannot happen in Texas Hold 'em):

J-J-J-6-3 and K-K-Q-Q-8.

The former beats the latter since three of a kind is stronger than two pairs.

J-J-J-6-3 and K-Q-8-8-8.

Since both are three of a kind, the triples are considered first, J and 8 in this case.
J is higher, hence the former is a stronger hand.
The remaining cards, 6-3 and K-Q, are not considered as the tie is already broken.

Q-J-8-6-3 and Q-J-8-5-3.

Both are high cards, assuming hands not of a single suit (i.e. flush).
The three highest-ranked cards Q-J-8 are the same, so the fourth highest are compared.
The former is stronger since 6 is higher than 5.

9-9-Q-7-2 and 9-9-J-8-5.

Both are one pair, with the pair of the same rank (9).
So the remaining cards, Q-7-2 and J-8-5, are compared from the highest to the lowest,
and the former wins as Q is higher than J.

Now suppose you are playing a game of Texas Hold 'em with one opponent, and the hole cards and the flop have already been dealt.
You are surprisingly telepathic and able to know the cards the opponent has.
Your ability is not, however, as strong as you can predict which the turn and the river will be.

Your task is to write a program that calculates the probability of your winning the game,
assuming the turn and the river are chosen uniformly randomly from the remaining cards.
You and the opponent always have to choose the hand strongest possible.
Ties should be included in the calculation, i.e. should be counted as losses.

Input

The input consists of multiple datasets, each of which has the following format:

YourCard_1 YourCard_2
OpponentCard_1 OpponentCard_2
CommunityCard_1 CommunityCard_2 CommunityCard_3

Each dataset consists of three lines.
The first and second lines contain the hole cards of yours and the opponent's respectively.
The third line contains the flop, i.e. the first three community cards.
These cards are separated by spaces.

Each card is represented by two characters.
The first one indicates the suit: S (spades), H (hearts), D (diamonds) or C (clubs).
The second one indicates the rank: A, K, Q, J, T (10) or 9-2.

The end of the input is indicated by a line with #.
This should not be processed.

Output

Print the probability in a line.
The number may contain an arbitrary number of digits after the decimal point,
but should not contain an absolute error greater than 10^{-6}.

Sample Input

SA SK
DA CA
SQ SJ ST
SA HA
D2 C3
H4 S5 DA
HA D9
H6 C9
H3 H4 H5
#

Output for the Sample Input

1.00000000000000000000
0.34444444444444444198
0.63030303030303025391

Solution in C (judge verdict: Wrong Answer)

/*
  AOJ 2535
  Title:Texas hold 'em
  @kankichi573
*/
#include <stdio.h>
#include <string.h>
#define max(x,y) (((x)>(y))?(x):(y))
int your_card[2];
int oppo_card[2];
int comm_card[3];
int turn,river;
char suits_str[]="SHCD";
char rank_str[]="A23456789TJQK";
#define ROYAL 10
#define STFL 9
#define FOURCARDS 8
#define FULLHOUSE 7
#define FLASH 6
#define STRAIGHT 5 
#define THREECARDS 4
#define TOWPAIR 3
#define ONEPAIR 2
#define NOPAIR 1

int ncomp[11]={0,5,4,3,3,1,5,2,2,1,0}; // zero base

int comp_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}

int is_straight(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i;

  for(i=13;i>=4;i--)
    if(rank_dist[i] && rank_dist[i-1] && rank_dist[i-2] && 
       rank_dist[i-3] && rank_dist[i-4] )
      {
	flag[0]=i;
	return(-1);
      }
  return(0);
}

int is_royal(int cards[7],int flag[5],int rank_dist[14],int s)
{
  int i,f,r;

  if(s<0)
    return(0);

  for(i=0,f=0;i<7;i++)
    {
      if(cards[i]/13==s)
	{
	  r=cards[i]%13;
	  if(r==0||(r>8 && r < 13))
	    f++;
	}
    }
  return(f==5);
}
int is_stfl(int cards[7],int flag[5],int rank_dist[14],int s)
{
  int i,p;

  if(s<0)
    return(0);
  qsort(cards,7,sizeof(int),comp_int);
  for(i=6;i>3;i--)
    {
      if(cards[i]%13 >= 4 && cards[i]-4==cards[i-4])
	{
	  flag[0]=cards[i]%13;
	  return(1);
	}
    }
  return(0);
}
int is_flash(int cards[7],int flag[5],int rank_dist[14],int s)
{
  int i,p,j,rank;

  if(s<0)
    return(0);
  qsort(cards,7,sizeof(int),comp_int);
  for(i=6,p=0;i>=0;i--)
    {
      if(cards[i]/13==s)
	{
	  rank=cards[i]%13;
	  if(rank==0) //ace no shori
	    {
	      if(p>0)
		for(j=p-1;j>=0;j--)
		  flag[j+1]=flag[j];
	      flag[0]=13;
	      p++;
	    }
	  else
	    flag[p++]=rank;
	}
    }


  return(1);
}
int is_fourcards(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,npair,p;
  p=npair=0;
  for(i=13;i>=1;i--)
    if(rank_dist[i]==4)
      {
	npair++;
	flag[0]=i;
      }
  if(npair<1)
    return(0);
  npair=0;
  for(i=13;i>=0;i--)
    if(rank_dist[i]>0 && rank_dist[i]<4)
      {
	flag[1]=i;
	break;
      }
    return(1);
}
int is_fullhouse(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,npair,p;
  p=npair=0;
  for(i=13;i>=1;i--)
    if(rank_dist[i]==3)
      {
	npair++;
	flag[0]=i;
	break;
      }
  if(npair<1)
    return(0);

  npair=0;
  for(i=13;i>=1;i--)
    if(rank_dist[i]>=2 && i!=flag[0])
      {
	npair++;
	flag[1]=i;
	break;
      }
  if(npair<1)
    return(0);
  else
    return(1);
}
int is_threecards(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,npair;
  npair=0;
  for(i=13;i>=1;i--)
    if(rank_dist[i]==3)
      {
	npair++;
	flag[0]=i;
      }
  if(npair<1)
    return(0);
  for(i=13;i>=1;i--)
    if(rank_dist[i]==1)
      {
	flag[1]=i;
	break;
      }
  for(i=13;i>=1;i--)
    if(rank_dist[i]==1 && i<flag[1])
      {
	flag[2]=i;
	break;
      }
  return(1);
}
int is_twopairs(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,npair,p;
  p=npair=0;
  for(i=13;i>=1;i--)
    if(rank_dist[i]==2)
      {
	npair++;
	flag[p]=i;
	p++;
      }
  if(npair<2)
    return(0);
  for(i=13;i>=1;i--)
    if(rank_dist[i]==1)
      {
	flag[2]=i;
	break;
      }
  return(1);
}
int is_nopair(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,p;
  for(i=13;i>=1;i--)
    {
      if(rank_dist[i]>=2)
	return(0);
      if(rank_dist[i]==1)
	{
	  flag[p]=i;
	  p++;
	}
    }
  return(1);
}
int is_onepair(int cards[7],int flag[5],int rank_dist[14],int flash_suit)
{
  int i,npair,p;
  for(i=13,npair=0;i>=1;i--)
    if(rank_dist[i]==2)
      {
	npair++;
	flag[0]=i;
      }
  if(npair!=1)
    return(0);
  for(i=13,p=1;i>=1;i--)
    if(rank_dist[i]==1)
      {
	flag[p]=i;
	p++;
      }
  return(1);
}

int ((*func[11])(int *,int *,int *,int))=
{NULL,is_nopair,is_onepair,is_twopairs,
 is_threecards,is_straight,is_flash,
 is_fullhouse,is_fourcards,is_stfl,is_royal};

void dump(int suit_dist[],int rank_dist[])
{
  int i;
  for(i=0;i<4;i++)
    printf("%d:",suit_dist[i]);
  printf("\n");
  for(i=0;i<13;i++)
    printf("%d:",rank_dist[i]);
  printf("\n");
}

int yaku(int cards[7],int flag[5])
{
  int i,fl,ret;
  int rank_dist[14]; //ace occurs twice
  int suit_dist[4];
  int flash_suit;

  for(i=0;i<13;i++)
    rank_dist[i]=0;
  for(i=0;i<4;i++)
    suit_dist[i]=0;
  for(i=0;i<7;i++)
    {
      rank_dist[cards[i]%13]++;
      suit_dist[cards[i]/13]++;
    }
  rank_dist[13]=rank_dist[0];

  for(i=0,flash_suit=-1;i<4;i++)
    {
      if(suit_dist[i]>=5)
	flash_suit=i;
    }
#ifdef DEBUG
  dump(suit_dist,rank_dist);
#endif

  for(i=10;i>=1;i--)
    {
      ret=(*func[i])(cards,flag,rank_dist,flash_suit);
      if(ret)
	return(i);
    }
  return(1);
}
int comp2(int yh[7],int oh[7])
{
  int yf[5],of[5],i;
  int yyaku=yaku(yh,yf),oyaku=yaku(oh,of);

  if(yyaku>oyaku)
    return(1);
  if(yyaku<oyaku)
    return(0);
  for(i=0;i<ncomp[yyaku];i++)
    {
      //printf("::%d %d\n",yf[i]+1,of[i]+1);
      if(yf[i]>of[i])  //kicker
	return(1);
      if(yf[i]<of[i])
	return(0);
    }
  return(-1);
}
int compare_hand(int you[2],int oppo[2],int com[3],int turn,int river)
{
  int i;

  int your_hand[7],oppo_hand[7];
  for(i=0;i<2;i++)
      {
	your_hand[i]=you[i];
	oppo_hand[i]=oppo[i];
      }
  for(i=0;i<3;i++)
    your_hand[2+i]=oppo_hand[2+i]=com[i];
  your_hand[5]=oppo_hand[5] = turn;
  your_hand[6]=oppo_hand[6] = river;
  return(comp2(your_hand,oppo_hand));
}
int suit2num(char suit)
{
  return(strchr(suits_str,suit)-suits_str);
}
int rank2num(char rank)
{
  return(strchr(rank_str,rank)-rank_str);
}
int check_duplicate(int c,int your_card[],int oppo_card[],int comm_card[])
{
  int i;
  for(i=0;i<2;i++)
    if(c==your_card[i])
      return(-1);
  for(i=0;i<2;i++)
    if(c==oppo_card[i])
      return(-1);
  for(i=0;i<3;i++)
    if(c==comm_card[i])
      return(-1);
  return(0);
}
int check_duplicate2(int c,int c2,int your_card[],
int oppo_card[],int comm_card[])
{
  if(c==c2)
    return(-1);
  else    
    return(check_duplicate(c2,your_card,oppo_card,comm_card));
}
#ifdef DEBUG


main()
{
  int hand[2][7],flag[5];
  int i,ns,nr,y,j,ret;
  char card[2];
  
  for(j=0;j<2;j++)
    {

      for(i=0;i<7;i++)
	{
	  scanf("%s",card);

	  if(card[0]=='#')
	    goto END;
	  
	  ns=suit2num(card[0]);
	  nr=rank2num(card[1]);
	  hand[j][i]=13*ns+nr;
	}
    }
  ret=comp2(&hand[0][0],&hand[1][0]);
  printf("win=%s\n",(ret>0)?"WIN":((ret==0)?"LOSE":"TIE"));
 END:
  return(0);
}

#else

main()
{
  int i,j,bunbo,bunsi;
  char buf[2],suit,rank;
  
  while(1)
    {
      scanf("%s",&buf[0]); 
      if('#'==buf[0])
	break;

      your_card[0]=13*suit2num(buf[0])+rank2num(buf[1]);
      scanf("%s",&buf[0]);	
      your_card[1]=13*suit2num(buf[0])+rank2num(buf[1]);

      for(i=0;i<2;i++)
	{
	  scanf("%s",&buf[0]);	
	  oppo_card[i]=13*suit2num(buf[0])+rank2num(buf[1]);
	}
      for(i=0;i<3;i++)
	{
	  scanf("%s",&buf[0]);	
	  comm_card[i]=13*suit2num(buf[0])+rank2num(buf[1]);
	}
      bunbo=bunsi=0;
      for(i=0;i<52;i++)
	{
	  if(check_duplicate(i,your_card,oppo_card,comm_card))
	     continue;
	  turn=i;
	  for(j=0;j<52;j++)
	    {
	      if(check_duplicate2(i,j,your_card,oppo_card,comm_card))
		continue;
	      river=j;
	      bunbo++;
	      if(compare_hand(your_card,oppo_card,comm_card,turn,river)>0)
		bunsi++;
	    }
	}
      printf("%lf\n",((double)bunsi)/((double)bunbo));
    }
  return(0);
}

#endif