Problem p01335 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
int h[5], s[5];
int v[13][4];
int m[128];
int k[10];
int H() {
  for (int i = 1; i < 5; ++i)
    if (s[i] - s[i - 1])
      return 0;
  return 1;
}
int X() { return h[1] + 1 == h[2] && h[2] + 1 == h[3] && h[3] + 1 == h[4]; }
int S() { return X() && h[0] + 1 == h[1]; }
int R() { return X() && h[0] == 1 && h[4] == 13; }
int F() {
  if (H()) {
    if (R())
      return 9;
    if (S())
      return 8;
    return 5;
  } else {
    if (h[0] == h[3] || h[1] == h[4])
      return 7;
    if (h[0] == h[1] && h[2] == h[4] || h[0] == h[2] && h[3] == h[4])
      return 6;
    if (S() || R())
      return 4;
    if (h[0] == h[2] || h[1] == h[3] || h[2] == h[4])
      return 3;
    if (h[0] == h[1])
      if (h[2] == h[3] || h[3] == h[4])
        return 2;
    if (h[1] == h[2] && h[3] == h[4])
      return 2;
    if (h[0] == h[1] || h[1] == h[2] || h[2] == h[3] || h[3] == h[4])
      return 1;
    return 0;
  }
}
int main() {
  int N, i, j, r, x = 0;
  char c, d;
  m['A'] = 1;
  for (i = 0; i < 10; ++i)
    m[i + '0'] = i;
  for (i = 0; i < 4; ++i)
    m["TJQK"[i]] = i + 10, m["SCHD"[i]] = i;
  while (scanf("%d", &N), N) {
    if (x++)
      puts("");
    for (i = 0; i < 4; ++i)
      for (j = 0; j < 13; ++j)
        scanf("%d", &v[j][i]);
    for (i = 1; i < 10; ++i)
      scanf("%d", &k[i]);
    while (N--) {
      for (r = i = 0; i < 5; ++i) {
        scanf(" %c %c", &c, &d);
        h[i] = m[c];
        s[i] = m[d];
        r += v[h[i] - 1][s[i]];
      }
      sort(h, h + 5);
      printf("%d\n", r * k[F()]);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int h[5], s[5];
int v[13][4];
int m[128];
int k[10];
int H() {
  for (int i = 1; i < 5; ++i)
    if (s[i] - s[i - 1])
      return 0;
  return 1;
}
int X() { return h[1] + 1 == h[2] && h[2] + 1 == h[3] && h[3] + 1 == h[4]; }
int S() { return X() && h[0] + 1 == h[1]; }
int R() { return X() && h[0] == 1 && h[4] == 13; }
int F() {
  if (H()) {
    if (R())
      return 9;
    if (S())
      return 8;
    return 5;
  } else {
    if (h[0] == h[3] || h[1] == h[4])
      return 7;
    if (h[0] == h[1] && h[2] == h[4] || h[0] == h[2] && h[3] == h[4])
      return 6;
    if (S() || R())
      return 4;
    if (h[0] == h[2] || h[1] == h[3] || h[2] == h[4])
      return 3;
    if (h[0] == h[1])
      if (h[2] == h[3] || h[3] == h[4])
        return 2;
    if (h[1] == h[2] && h[3] == h[4])
      return 2;
    if (h[0] == h[1] || h[1] == h[2] || h[2] == h[3] || h[3] == h[4])
      return 1;
    return 0;
  }
}
int main() {
  int N, i, j, r, x = 0;
  char c, d;
  m['A'] = 1;
  for (i = 0; i < 10; ++i)
    m[i + '0'] = i;
  for (i = 0; i < 4; ++i)
    m["TJQK"[i]] = i + 10, m["SCHD"[i]] = i;
  while (scanf("%d", &N) >= 0) {
    if (x++)
      puts("");
    for (i = 0; i < 4; ++i)
      for (j = 0; j < 13; ++j)
        scanf("%d", &v[j][i]);
    for (i = 1; i < 10; ++i)
      scanf("%d", &k[i]);
    while (N--) {
      for (r = i = 0; i < 5; ++i) {
        scanf(" %c %c", &c, &d);
        h[i] = m[c];
        s[i] = m[d];
        r += v[h[i] - 1][s[i]];
      }
      sort(h, h + 5);
      printf("%d\n", r * k[F()]);
    }
  }
  return 0;
}