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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int score[14][4];
int rate[10];

typedef pair<int, int> card;
typedef vector<card> cards;

int toNum(char a) {
  if (isdigit(a))
    return a - '0';
  if (a == 'T')
    return 10;
  if (a == 'J')
    return 11;
  if (a == 'Q')
    return 12;
  if (a == 'K')
    return 13;
  return a - 'A' + 1;
}

int toNum2(char a) {
  if (a == 'S')
    return 0;
  if (a == 'C')
    return 1;
  if (a == 'H')
    return 2;

  return 3;
}

cards input() {
  cards in;
  for (int i = 0; i < 5; i++) {
    string s;
    cin >> s;
    in.push_back(make_pair(toNum(s[0]), toNum2(s[1])));
  }

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

  return in;
}

vector<int> calcPair(cards in) {
  vector<int> ret;
  int num[14] = {0};
  for (int i = 0; i < 5; i++) {
    num[in[i].first]++;
  }

  for (int i = 1; i <= 13; i++) {
    if (num[i] != 0)
      ret.push_back(num[i]);
  }

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

  return ret;
}

bool isFlash(cards in) {
  for (int i = 1; i < 5; i++)
    if (in[0].second != in[i].second)
      return false;

  return true;
}

int isStraight(cards in, vector<int> pr) {
  if (pr.size() != 5)
    return 0;

  int num[14] = {0};
  for (int i = 0; i < 5; i++) {
    num[in[i].first + (4 - i)]++;
  }

  if (num[in[4].first] == 5)
    return 1;
  if (num[in[4].first] == 4 && in[4].first == 13 && num[5])
    return 2;

  return 0;
}

int total(cards in) {
  int ret = 0;
  for (int i = 0; i < 5; i++)
    ret += score[in[i].first][in[i].second];

  return ret;
}

int calc(cards in) {
  vector<int> pr = calcPair(in);
  int ret = 0, sz = pr.size();

  if (pr[sz - 1] == 2)
    ret = 1;
  if (pr[sz - 1] == 2 && pr[sz - 2] == 2)
    ret = 2;
  if (pr[sz - 1] == 3)
    ret = 3;
  if (isStraight(in, pr) != 0)
    ret = 4;
  if (isFlash(in))
    ret = 5;
  if (pr[sz - 2] == 2 && pr[sz - 1] == 3)
    ret = 6;
  if (pr[sz - 1] == 4)
    ret = 7;
  if (isStraight(in, pr) != 0 && isFlash(in))
    ret = 8;
  if (isStraight(in, pr) == 2 && isFlash(in))
    ret = 9;

  return total(in) * rate[ret];
}

int main() {
  bool flg = true;
  int N;
  while (cin >> N) {
    if (flg)
      flg = false;
    else
      cout << endl;

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 13; j++)
        cin >> score[j + 1][i];

    rate[0] = 0;
    for (int i = 0; i < 9; i++)
      cin >> rate[i + 1];

    for (int i = 0; i < N; i++) {
      cards cs = input();
      cout << calc(cs) << endl;
    }
  }
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int score[14][4];
int rate[10];

typedef pair<int, int> card;
typedef vector<card> cards;

int toNum(char a) {
  if (isdigit(a))
    return a - '0';
  if (a == 'T')
    return 10;
  if (a == 'J')
    return 11;
  if (a == 'Q')
    return 12;
  if (a == 'K')
    return 13;
  return a - 'A' + 1;
}

int toNum2(char a) {
  if (a == 'S')
    return 0;
  if (a == 'C')
    return 1;
  if (a == 'H')
    return 2;

  return 3;
}

cards input() {
  cards in;
  for (int i = 0; i < 5; i++) {
    string s;
    cin >> s;
    in.push_back(make_pair(toNum(s[0]), toNum2(s[1])));
  }

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

  return in;
}

vector<int> calcPair(cards in) {
  vector<int> ret;
  int num[14] = {0};
  for (int i = 0; i < 5; i++) {
    num[in[i].first]++;
  }

  for (int i = 1; i <= 13; i++) {
    if (num[i] > 1)
      ret.push_back(num[i]);
  }

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

  return ret;
}

bool isFlash(cards in) {
  for (int i = 1; i < 5; i++)
    if (in[0].second != in[i].second)
      return false;

  return true;
}

int isStraight(cards in) {
  int num[14] = {0};
  for (int i = 0; i < 5; i++) {
    num[in[i].first + (4 - i)]++;
  }

  if (num[in[4].first] == 5)
    return 1;
  if (num[in[4].first] == 4 && num[5])
    return 2;

  return 0;
}

int total(cards in) {
  int ret = 0;
  for (int i = 0; i < 5; i++)
    ret += score[in[i].first][in[i].second];

  return ret;
}

int calc(cards in) {
  vector<int> pr = calcPair(in);
  int ret = 0, sz = pr.size();

  if (sz != 0 && pr[sz - 1] == 2)
    ret = 1;
  if (sz == 2 && pr[0] == pr[1])
    ret = 2;
  if (sz != 0 && pr[sz - 1] == 3)
    ret = 3;
  if (isStraight(in) != 0)
    ret = 4;
  if (isFlash(in))
    ret = 5;
  if (sz == 2 && pr[0] == 2 && pr[1] == 3)
    ret = 6;
  if (sz != 0 && pr[sz - 1] == 4)
    ret = 7;
  if (isStraight(in) != 0 && isFlash(in))
    ret = 8;
  if (isStraight(in) == 2 && isFlash(in))
    ret = 9;

  return total(in) * rate[ret];
}

int main() {
  bool flg = true;
  int N;
  while (cin >> N) {
    if (flg)
      flg = false;
    else
      cout << endl;

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 13; j++)
        cin >> score[j + 1][i];

    rate[0] = 0;
    for (int i = 0; i < 9; i++)
      cin >> rate[i + 1];

    for (int i = 0; i < N; i++) {
      cards cs = input();
      cout << calc(cs) << endl;
    }
  }
}