Problem p01647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

char c1[4] = {'S', 'H', 'D', 'C'};
char c2[13] = {'>', '=', '<', ';', ':', '9', '8', '7', '6', '5', '4', '3', '2'};

vector<string> cards;
set<string> used;

void init() {
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 13; j++) {
      string s, t;
      s += c1[i];
      t += c2[j];
      cards.push_back(s + t);
    }
}

string change(string s) {

  if (s[1] == 'A')
    s[1] = '>';
  if (s[1] == 'K')
    s[1] = '=';
  if (s[1] == 'Q')
    s[1] = '<';
  if (s[1] == 'J')
    s[1] = ';';
  if (s[1] == 'T')
    s[1] = ':';

  return s;
}

bool is0(vector<string> &c) { // Royal straight flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  sort(c.begin(), c.end(), greater<string>());

  for (int i = 0; i < c.size(); i++)
    if (c[i][1] != c2[i])
      return false;

  return true;
}

bool is1(vector<string> &c) { // Straight flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  sort(c.begin(), c.end());

  if (c[0][1] == '2' && c[1][1] == '3' && c[2][1] == '4' && c[3][1] == '5' &&
      c[4][1] == '>')
    return true;

  for (int i = 0; i < c.size() - 1; i++)
    if (c[i][1] + 1 != c[i + 1][1])
      return false;

  return true;
}

bool is2(vector<string> &c) { // Four of a kind

  for (int i = 0; i < c.size(); i++) {

    int cnt = 0;

    for (int j = 0; j < c.size(); j++)
      if (c[i][1] == c[j][1])
        cnt++;

    if (cnt == 4)
      return true;
  }

  return false;
}

bool is3(vector<string> &c) { // Full house

  for (int i = 0; i < c.size(); i++) {

    int cnt = 0;

    for (int j = 0; j < c.size(); j++)
      if (c[i][1] == c[j][1])
        cnt++;

    if (cnt == 3) {

      vector<char> comp;

      for (int j = 0; j < c.size(); j++)
        if (c[i][1] != c[j][1])
          comp.push_back(c[j][1]);

      if (comp[0] == comp[1])
        return true;
    }
  }

  return false;
}

bool is4(vector<string> &c) { // Flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  return true;
}

bool is5(vector<string> &c) { // Straight

  vector<char> A;

  for (int i = 0; i < c.size(); i++)
    A.push_back(c[i][1]);

  sort(A.begin(), A.end());

  if (A[0] == '2' && A[1] == '3' && A[2] == '4' && A[3] == '5' && A[4] == '>')
    return true;

  for (int i = 0; i < A.size() - 1; i++)
    if (A[i] + 1 != A[i + 1])
      return false;

  return true;
}

bool is6(vector<string> &c) { // Three of a kind

  for (int i = 0; i < c.size(); i++)

    for (int j = i + 1; j < c.size(); j++)

      for (int k = j + 1; k < c.size(); k++)

        if (c[i][1] == c[j][1] && c[j][1] == c[k][1])
          return true;

  return false;
}

bool is7(vector<string> &c) { // Two pairs

  int C[400] = {};

  for (int i = 0; i < c.size(); i++)
    C[c[i][1]]++;

  int cnt = 0;

  for (int i = 0; i < 400; i++)
    if (C[i] == 2)
      cnt++;

  return cnt == 2;
}

bool is8(vector<string> &c) { // One pair

  for (int i = 0; i < c.size(); i++)

    for (int j = i + 1; j < c.size(); j++)

      if (c[i][1] == c[j][1])
        return true;

  return false;
}

int flag;

vector<string> MAX1(vector<string> &A, vector<string> &B) { // Straight flush

  priority_queue<char> a, b;

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  if (A[0][1] == '2' && A[4][1] == '>')
    a.pop();
  if (B[0][1] == '2' && B[4][1] == '>')
    b.pop();

  if (a.top() > b.top()) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX2(vector<string> &A, vector<string> &B) { // Four of a kind

  int C1[400] = {}, C2[400] = {};

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  char a, b, c, d;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 4)
      a = A[i][1];
    else
      b = A[i][1];

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 4)
      c = B[i][1];
    else
      d = B[i][1];

  if (a > c) {
    flag = 1;
    return A;
  }
  if (a < c)
    return B;
  if (b > d) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX3(vector<string> &A, vector<string> &B) { // Full house

  int C1[400] = {}, C2[400] = {};

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  char a, b, c, d;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 3)
      a = A[i][1];
    else
      b = A[i][1];

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 3)
      c = B[i][1];
    else
      d = B[i][1];

  if (a > c) {
    flag = 1;
    return A;
  }
  if (a < c)
    return B;
  if (b > d) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX4(vector<string> &A, vector<string> &B) { // Flush

  priority_queue<char> a, b;

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX5(vector<string> &A, vector<string> &B) { // Straight

  priority_queue<char> a, b;

  int flaga = 0, flagb = 0;

  for (int i = 0; i < A.size(); i++) {
    a.push(A[i][1]);
    if (A[i][1] == '2')
      flaga = 1;
  }

  for (int i = 0; i < B.size(); i++) {
    b.push(B[i][1]);
    if (B[i][1] == '2')
      flagb = 1;
  }

  if (flaga && a.top() == '>')
    a.pop();

  if (flagb && b.top() == '>')
    b.pop();

  if (a.top() > b.top()) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX6(vector<string> &A, vector<string> &B) { // Three of a kind

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 3)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 3)
      d = B[i][1];
    else
      b.push(B[i][1]);

  if (c > d) {
    flag = 1;
    return A;
  }
  if (c < d)
    return B;

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX7(vector<string> &A, vector<string> &B) { // Two pairs

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 1)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 1)
      d = B[i][1];
    else
      b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  if (c > d) {
    flag = 1;
    return A;
  }

  return B;
}

vector<string> MAX8(vector<string> &A, vector<string> &B) { // One pair

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 2)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 2)
      d = B[i][1];
    else
      b.push(B[i][1]);

  if (c > d) {
    flag = 1;
    return A;
  }
  if (c < d)
    return B;

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX9(vector<string> &A, vector<string> &B) { // High card

  priority_queue<char> a, b;

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> memo;

int judge(vector<string> &list) {

  memo.clear();

  int res = 9;

  for (int i = 0; i < list.size(); i++) {

    for (int j = i + 1; j < list.size(); j++) {

      vector<string> five;

      for (int k = 0; k < list.size(); k++)
        if (i != k && j != k)
          five.push_back(list[k]);

      if (memo.size() == 0)
        memo = five;

      if (is0(five)) {
        if (res > 0) {
          res = 0;
          memo = five;
        }
      } else if (is1(five)) {
        if (res > 1) {
          res = 1;
          memo = five;
        }
        if (res == 1)
          memo = MAX1(memo, five);
      } else if (is2(five)) {
        if (res > 2) {
          res = 2;
          memo = five;
        }
        if (res == 2)
          memo = MAX2(memo, five);
      } else if (is3(five)) {
        if (res > 3) {
          res = 3;
          memo = five;
        }
        if (res == 3)
          memo = MAX3(memo, five);
      } else if (is4(five)) {
        if (res > 4) {
          res = 4;
          memo = five;
        }
        if (res == 4)
          memo = MAX4(memo, five);
      } else if (is5(five)) {
        if (res > 5) {
          res = 5;
          memo = five;
        }
        if (res == 5)
          memo = MAX5(memo, five);
      } else if (is6(five)) {
        if (res > 6) {
          res = 6;
          memo = five;
        }
        if (res == 6)
          memo = MAX6(memo, five);
      } else if (is7(five)) {
        if (res > 7) {
          res = 7;
          memo = five;
        }
        if (res == 7)
          memo = MAX7(memo, five);
      } else if (is8(five)) {
        if (res > 8) {
          res = 8;
          memo = five;
        }
        if (res == 8)
          memo = MAX8(memo, five);
      } else if (res == 9)
        memo = MAX9(memo, five);
    }
  }

  return res;
}

bool judge2(vector<string> A, vector<string> B, int c) {

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  if (!c)
    return false;

  flag = 0;

  if (c == 1)
    MAX1(A, B);
  if (c == 2)
    MAX2(A, B);
  if (c == 3)
    MAX3(A, B);
  if (c == 4)
    MAX4(A, B);
  if (c == 5)
    MAX5(A, B);
  if (c == 6)
    MAX6(A, B);
  if (c == 7)
    MAX7(A, B);
  if (c == 8)
    MAX8(A, B);
  if (c == 9)
    MAX9(A, B);

  return flag;
}

vector<string> A, B, C, R;

void solve() {

  int sum = 0, cnt = 0;

  for (int i = 0; i < R.size(); i++) {

    for (int j = i + 1; j < R.size(); j++) {

      vector<string> Alist, Blist;

      for (int k = 0; k < A.size(); k++)
        Alist.push_back(A[k]);
      for (int k = 0; k < C.size(); k++)
        Alist.push_back(C[k]);

      Alist.push_back(R[i]);
      Alist.push_back(R[j]);

      int a = judge(Alist);
      vector<string> Amemo = memo;

      for (int k = 0; k < B.size(); k++)
        Blist.push_back(B[k]);
      for (int k = 0; k < C.size(); k++)
        Blist.push_back(C[k]);
      Blist.push_back(R[i]);
      Blist.push_back(R[j]);

      int b = judge(Blist);
      vector<string> Bmemo = memo;

      if (a < b)
        cnt++;
      if (a == b && judge2(Amemo, Bmemo, a))
        cnt++;

      sum++;
    }
  }

  printf("%.20f\\n", 1.0 * cnt / sum);
}

int main() {

  init();

  while (1) {

    A.clear();
    B.clear();
    C.clear();
    R.clear();
    used.clear();

    for (int i = 0; i < 2; i++) {
      string s;
      cin >> s;
      if (s == "#")
        return 0;
      s = change(s);
      A.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < 2; i++) {
      string s;
      cin >> s;
      s = change(s);
      B.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < 3; i++) {
      string s;
      cin >> s;
      s = change(s);
      C.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < cards.size(); i++)

      if (!used.count(cards[i]))
        R.push_back(cards[i]);

    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

char c1[4] = {'S', 'H', 'D', 'C'};
char c2[13] = {'>', '=', '<', ';', ':', '9', '8', '7', '6', '5', '4', '3', '2'};

vector<string> cards;
set<string> used;

void init() {
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 13; j++) {
      string s, t;
      s += c1[i];
      t += c2[j];
      cards.push_back(s + t);
    }
}

string change(string s) {

  if (s[1] == 'A')
    s[1] = '>';
  if (s[1] == 'K')
    s[1] = '=';
  if (s[1] == 'Q')
    s[1] = '<';
  if (s[1] == 'J')
    s[1] = ';';
  if (s[1] == 'T')
    s[1] = ':';

  return s;
}

bool is0(vector<string> c) { // Royal straight flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  sort(c.begin(), c.end(), greater<string>());

  for (int i = 0; i < c.size(); i++)
    if (c[i][1] != c2[i])
      return false;

  return true;
}

bool is1(vector<string> c) { // Straight flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  sort(c.begin(), c.end());

  if (c[0][1] == '2' && c[1][1] == '3' && c[2][1] == '4' && c[3][1] == '5' &&
      c[4][1] == '>')
    return true;

  for (int i = 0; i < c.size() - 1; i++)
    if (c[i][1] + 1 != c[i + 1][1])
      return false;

  return true;
}

bool is2(vector<string> c) { // Four of a kind

  for (int i = 0; i < c.size(); i++) {

    int cnt = 0;

    for (int j = 0; j < c.size(); j++)
      if (c[i][1] == c[j][1])
        cnt++;

    if (cnt == 4)
      return true;
  }

  return false;
}

bool is3(vector<string> c) { // Full house

  for (int i = 0; i < c.size(); i++) {

    int cnt = 0;

    for (int j = 0; j < c.size(); j++)
      if (c[i][1] == c[j][1])
        cnt++;

    if (cnt == 3) {

      vector<char> comp;

      for (int j = 0; j < c.size(); j++)
        if (c[i][1] != c[j][1])
          comp.push_back(c[j][1]);

      if (comp[0] == comp[1])
        return true;
    }
  }

  return false;
}

bool is4(vector<string> c) { // Flush

  char suit = c[0][0];

  for (int i = 0; i < c.size(); i++)
    if (suit != c[i][0])
      return false;

  return true;
}

bool is5(vector<string> c) { // Straight

  vector<char> A;

  for (int i = 0; i < c.size(); i++)
    A.push_back(c[i][1]);

  sort(A.begin(), A.end());

  if (A[0] == '2' && A[1] == '3' && A[2] == '4' && A[3] == '5' && A[4] == '>')
    return true;

  for (int i = 0; i < A.size() - 1; i++)
    if (A[i] + 1 != A[i + 1])
      return false;

  return true;
}

bool is6(vector<string> c) { // Three of a kind

  for (int i = 0; i < c.size(); i++)

    for (int j = i + 1; j < c.size(); j++)

      for (int k = j + 1; k < c.size(); k++)

        if (c[i][1] == c[j][1] && c[j][1] == c[k][1])
          return true;

  return false;
}

bool is7(vector<string> c) { // Two pairs

  int C[400] = {};

  for (int i = 0; i < c.size(); i++)
    C[c[i][1]]++;

  int cnt = 0;

  for (int i = 0; i < 400; i++)
    if (C[i] == 2)
      cnt++;

  return cnt == 2;
}

bool is8(vector<string> c) { // One pair

  for (int i = 0; i < c.size(); i++)

    for (int j = i + 1; j < c.size(); j++)

      if (c[i][1] == c[j][1])
        return true;

  return false;
}

int flag;

vector<string> MAX1(vector<string> A, vector<string> B) { // Straight flush

  priority_queue<char> a, b;

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  if (A[0][1] == '2' && A[4][1] == '>')
    a.pop();
  if (B[0][1] == '2' && B[4][1] == '>')
    b.pop();

  if (a.top() > b.top()) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX2(vector<string> A, vector<string> B) { // Four of a kind

  int C1[400] = {}, C2[400] = {};

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  char a, b, c, d;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 4)
      a = A[i][1];
    else
      b = A[i][1];

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 4)
      c = B[i][1];
    else
      d = B[i][1];

  if (a > c) {
    flag = 1;
    return A;
  }
  if (a < c)
    return B;
  if (b > d) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX3(vector<string> A, vector<string> B) { // Full house

  int C1[400] = {}, C2[400] = {};

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  char a, b, c, d;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 3)
      a = A[i][1];
    else
      b = A[i][1];

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 3)
      c = B[i][1];
    else
      d = B[i][1];

  if (a > c) {
    flag = 1;
    return A;
  }
  if (a < c)
    return B;
  if (b > d) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX4(vector<string> A, vector<string> B) { // Flush

  priority_queue<char> a, b;

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX5(vector<string> A, vector<string> B) { // Straight

  priority_queue<char> a, b;

  int flaga = 0, flagb = 0;

  for (int i = 0; i < A.size(); i++) {
    a.push(A[i][1]);
    if (A[i][1] == '2')
      flaga = 1;
  }

  for (int i = 0; i < B.size(); i++) {
    b.push(B[i][1]);
    if (B[i][1] == '2')
      flagb = 1;
  }

  if (flaga && a.top() == '>')
    a.pop();

  if (flagb && b.top() == '>')
    b.pop();

  if (a.top() > b.top()) {
    flag = 1;
    return A;
  }
  return B;
}

vector<string> MAX6(vector<string> A, vector<string> B) { // Three of a kind

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 3)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 3)
      d = B[i][1];
    else
      b.push(B[i][1]);

  if (c > d) {
    flag = 1;
    return A;
  }
  if (c < d)
    return B;

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX7(vector<string> A, vector<string> B) { // Two pairs

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 1)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 1)
      d = B[i][1];
    else
      b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  if (c > d) {
    flag = 1;
    return A;
  }

  return B;
}

vector<string> MAX8(vector<string> A, vector<string> B) { // One pair

  priority_queue<char> a, b;

  int C1[400] = {}, C2[400] = {};

  int c, d;

  for (int i = 0; i < A.size(); i++)
    C1[A[i][1]]++;
  for (int i = 0; i < B.size(); i++)
    C2[B[i][1]]++;

  for (int i = 0; i < A.size(); i++)
    if (C1[A[i][1]] == 2)
      c = A[i][1];
    else
      a.push(A[i][1]);

  for (int i = 0; i < B.size(); i++)
    if (C2[B[i][1]] == 2)
      d = B[i][1];
    else
      b.push(B[i][1]);

  if (c > d) {
    flag = 1;
    return A;
  }
  if (c < d)
    return B;

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> MAX9(vector<string> A, vector<string> B) { // High card

  priority_queue<char> a, b;

  for (int i = 0; i < A.size(); i++)
    a.push(A[i][1]);
  for (int i = 0; i < B.size(); i++)
    b.push(B[i][1]);

  while (!a.empty()) {

    if (a.top() > b.top()) {
      flag = 1;
      return A;
    }
    if (a.top() < b.top())
      return B;

    a.pop();
    b.pop();
  }

  return A;
}

vector<string> memo;

int judge(vector<string> list) {

  memo.clear();

  int res = 9;

  for (int i = 0; i < list.size(); i++) {

    for (int j = i + 1; j < list.size(); j++) {

      vector<string> five;

      for (int k = 0; k < list.size(); k++)
        if (i != k && j != k)
          five.push_back(list[k]);

      if (memo.size() == 0)
        memo = five;

      if (is0(five)) {
        if (res > 0) {
          res = 0;
          memo = five;
        }
      } else if (is1(five)) {
        if (res > 1) {
          res = 1;
          memo = five;
        }
        if (res == 1)
          memo = MAX1(memo, five);
      } else if (is2(five)) {
        if (res > 2) {
          res = 2;
          memo = five;
        }
        if (res == 2)
          memo = MAX2(memo, five);
      } else if (is3(five)) {
        if (res > 3) {
          res = 3;
          memo = five;
        }
        if (res == 3)
          memo = MAX3(memo, five);
      } else if (is4(five)) {
        if (res > 4) {
          res = 4;
          memo = five;
        }
        if (res == 4)
          memo = MAX4(memo, five);
      } else if (is5(five)) {
        if (res > 5) {
          res = 5;
          memo = five;
        }
        if (res == 5)
          memo = MAX5(memo, five);
      } else if (is6(five)) {
        if (res > 6) {
          res = 6;
          memo = five;
        }
        if (res == 6)
          memo = MAX6(memo, five);
      } else if (is7(five)) {
        if (res > 7) {
          res = 7;
          memo = five;
        }
        if (res == 7)
          memo = MAX7(memo, five);
      } else if (is8(five)) {
        if (res > 8) {
          res = 8;
          memo = five;
        }
        if (res == 8)
          memo = MAX8(memo, five);
      } else if (res == 9)
        memo = MAX9(memo, five);
    }
  }

  return res;
}

bool judge2(vector<string> A, vector<string> B, int c) {

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  if (!c)
    return false;

  flag = 0;

  if (c == 1)
    MAX1(A, B);
  if (c == 2)
    MAX2(A, B);
  if (c == 3)
    MAX3(A, B);
  if (c == 4)
    MAX4(A, B);
  if (c == 5)
    MAX5(A, B);
  if (c == 6)
    MAX6(A, B);
  if (c == 7)
    MAX7(A, B);
  if (c == 8)
    MAX8(A, B);
  if (c == 9)
    MAX9(A, B);

  return flag;
}

vector<string> A, B, C, R;

void solve() {

  int sum = 0, cnt = 0;

  for (int i = 0; i < R.size(); i++) {

    for (int j = i + 1; j < R.size(); j++) {

      vector<string> Alist, Blist;

      for (int k = 0; k < A.size(); k++)
        Alist.push_back(A[k]);
      for (int k = 0; k < C.size(); k++)
        Alist.push_back(C[k]);

      Alist.push_back(R[i]);
      Alist.push_back(R[j]);

      int a = judge(Alist);
      vector<string> Amemo = memo;

      for (int k = 0; k < B.size(); k++)
        Blist.push_back(B[k]);
      for (int k = 0; k < C.size(); k++)
        Blist.push_back(C[k]);
      Blist.push_back(R[i]);
      Blist.push_back(R[j]);

      int b = judge(Blist);
      vector<string> Bmemo = memo;

      if (a < b)
        cnt++;
      if (a == b && judge2(Amemo, Bmemo, a))
        cnt++;

      sum++;
    }
  }

  printf("%.20f\\n", 1.0 * cnt / sum);
}

int main() {

  init();

  while (1) {

    A.clear();
    B.clear();
    C.clear();
    R.clear();
    used.clear();

    for (int i = 0; i < 2; i++) {
      string s;
      cin >> s;
      if (s == "#")
        return 0;
      s = change(s);
      A.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < 2; i++) {
      string s;
      cin >> s;
      s = change(s);
      B.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < 3; i++) {
      string s;
      cin >> s;
      s = change(s);
      C.push_back(s);
      used.insert(s);
    }

    for (int i = 0; i < cards.size(); i++)

      if (!used.count(cards[i]))
        R.push_back(cards[i]);

    solve();
  }

  return 0;
}