Problem p01335 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

// #define	mp		make_pair
#define num first
#define suit second

using namespace std;

typedef pair<int, char> pic;

int base[4][14], ratio[9];

int isStraight(pic hand[5]) {
  sort(hand, hand + 5);
  if (hand[0].num == 1 && hand[1].num == 10 && hand[2].num == 11 &&
      hand[3].num == 12 && hand[4].num == 13)
    return 1;
  for (int i = 1; i < 5; i++)
    if (hand[0].num + i != hand[i].num)
      return -1;
  return hand[4].num;
}

bool isFlush(const pic hand[5]) {
  for (int i = 1; i < 5; i++)
    if (hand[0].suit != hand[i].suit)
      return false;
  return true;
}

bool isFourCard(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 4)
      return true;
  return false;
}

bool isFullHouse(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  bool _2 = false, _3 = false;
  for (int i = 1; i <= 13; i++) {
    if (buc[i] == 2)
      _2 = true;
    if (buc[i] == 3)
      _3 = true;
  }
  return _2 && _3;
}

bool isThreeCard(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 3)
      return true;
  return false;
}

bool isTwoPair(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  int _2 = 0;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 2)
      _2++;
  return _2 == 2;
}

bool isOnePair(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 2)
      return true;
  return false;
}

int calcHand(pic hand[5]) {
  int st = isStraight(hand);
  bool fl = isFlush(hand);
  if (st == 1 && fl)
    return ratio[8];
  if (st != -1 && fl)
    return ratio[7];
  if (isFourCard(hand))
    return ratio[6];
  if (isFullHouse(hand))
    return ratio[5];
  if (fl)
    return ratio[4];
  if (st != -1)
    return ratio[3];
  if (isThreeCard(hand))
    return ratio[2];
  if (isTwoPair(hand))
    return ratio[1];
  if (isOnePair(hand))
    return ratio[0];
  return 0;
}

int calcBase(const pic hand[5]) {
  static int suit2id[128];
  suit2id['S'] = 0;
  suit2id['C'] = 1;
  suit2id['H'] = 2;
  suit2id['D'] = 3;
  int point = 0;
  for (int i = 0; i < 5; i++)
    point += base[suit2id[hand[i].suit]][hand[i].num];
  return point;
}

int main() {
  for (int n, t = 0; ~scanf("%d", &n); t++) {
    if (t > 0)
      puts("");
    for (int i = 0; i < 4; i++)
      for (int j = 1; j <= 13; j++)
        scanf("%d", base[i] + j);
    for (int i = 0; i < 9; i++)
      scanf("%d ", ratio + i);
    while (n--) {
      pic hand[5];
      for (int i = 0; i < 5; i++) {
        char t;
        scanf("%c%c ", &t, &hand[i].suit);
        if (t == 'A')
          hand[i].num = 1;
        else if (t == 'T')
          hand[i].num = 10;
        else if (t == 'J')
          hand[i].num = 11;
        else if (t == 'Q')
          hand[i].num = 12;
        else if (t == 'K')
          hand[i].num = 13;
        else
          hand[i].num = t - '0';
      }
      printf("%d\\n", calcHand(hand) * calcBase(hand));
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>

// #define	mp		make_pair
#define num first
#define suit second

using namespace std;

typedef pair<int, char> pic;

int base[4][14], ratio[9];

int isStraight(pic hand[5]) {
  sort(hand, hand + 5);
  if (hand[0].num == 1 && hand[1].num == 10 && hand[2].num == 11 &&
      hand[3].num == 12 && hand[4].num == 13)
    return 1;
  for (int i = 1; i < 5; i++)
    if (hand[0].num + i != hand[i].num)
      return -1;
  return hand[4].num;
}

bool isFlush(const pic hand[5]) {
  for (int i = 1; i < 5; i++)
    if (hand[0].suit != hand[i].suit)
      return false;
  return true;
}

bool isFourCard(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 4)
      return true;
  return false;
}

bool isFullHouse(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  bool _2 = false, _3 = false;
  for (int i = 1; i <= 13; i++) {
    if (buc[i] == 2)
      _2 = true;
    if (buc[i] == 3)
      _3 = true;
  }
  return _2 && _3;
}

bool isThreeCard(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 3)
      return true;
  return false;
}

bool isTwoPair(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  int _2 = 0;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 2)
      _2++;
  return _2 == 2;
}

bool isOnePair(const pic hand[5]) {
  int buc[14] = {};
  for (int i = 0; i < 5; i++)
    buc[hand[i].num]++;
  for (int i = 1; i <= 13; i++)
    if (buc[i] == 2)
      return true;
  return false;
}

int calcHand(pic hand[5]) {
  int st = isStraight(hand);
  bool fl = isFlush(hand);
  if (st == 1 && fl)
    return ratio[8];
  if (st != -1 && fl)
    return ratio[7];
  if (isFourCard(hand))
    return ratio[6];
  if (isFullHouse(hand))
    return ratio[5];
  if (fl)
    return ratio[4];
  if (st != -1)
    return ratio[3];
  if (isThreeCard(hand))
    return ratio[2];
  if (isTwoPair(hand))
    return ratio[1];
  if (isOnePair(hand))
    return ratio[0];
  return 0;
}

int calcBase(const pic hand[5]) {
  static int suit2id[128];
  suit2id['S'] = 0;
  suit2id['C'] = 1;
  suit2id['H'] = 2;
  suit2id['D'] = 3;
  int point = 0;
  for (int i = 0; i < 5; i++)
    point += base[suit2id[hand[i].suit]][hand[i].num];
  return point;
}

int main() {
  for (int n; ~scanf("%d", &n);) {
    for (int i = 0; i < 4; i++)
      for (int j = 1; j <= 13; j++)
        scanf("%d", base[i] + j);
    for (int i = 0; i < 9; i++)
      scanf("%d ", ratio + i);
    while (n--) {
      pic hand[5];
      for (int i = 0; i < 5; i++) {
        int t;
        scanf("%c%c ", &t, &hand[i].suit);
        if (t == 'A')
          hand[i].num = 1;
        else if (t == 'T')
          hand[i].num = 10;
        else if (t == 'J')
          hand[i].num = 11;
        else if (t == 'Q')
          hand[i].num = 12;
        else if (t == 'K')
          hand[i].num = 13;
        else
          hand[i].num = t - '0';
      }
      printf("%d\\n", calcHand(hand) * calcBase(hand));
    }
    puts("");
  }
  return 0;
}