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

// 32
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int main() {
  bool ns = false;
  for (int n; cin >> n;) {
    const char *s = "SCHD";
    int b[128][13];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 13; j++) {
        cin >> b[s[i]][j];
      }
    }
    int m[10] = {};
    for (int i = 0; i < 9; i++) {
      cin >> m[i + 1];
    }
    if (ns) {
      cout << endl;
    }
    ns = true;
    while (n--) {
      const char *r = "A23456789TJQK";
      int apr[13] = {}, aps[128] = {};
      char rn[5], st[5];
      int rnd[5];
      for (int i = 0; i < 5; i++) {
        cin >> rn[i] >> st[i];
        rnd[i] = strchr(r, rn[i]) - r;
        apr[rnd[i]]++;
        aps[st[i]]++;
      }
      int p;
      p = count(apr, apr + 13, 2);
      int tf = *max_element(apr, apr + 13);
      if (tf == 3) {
        p = 3;
      }
      bool str = false;
      int sp;
      for (int i = 0; i < 13 - 3; i++) {
        int h = true;
        for (int j = 0; j < 5; j++) {
          h &= apr[(i + j) % 13];
        }
        if (h) {
          str = true;
          p = 4;
          sp = i;
        }
      }
      bool fl = find(aps, aps + 128, 5) != aps + 128;
      if (fl) {
        p = 5;
      }
      if (find(apr, apr + 13, 2) != apr + 13 &&
          find(apr, apr + 13, 3) != apr + 13) {
        p = 6;
      }
      if (tf == 4) {
        p = 7;
      }
      if (str && fl) {
        p = 8 + (sp == 9);
      }
      int bp = 0;
      for (int i = 0; i < 5; i++) {
        bp += b[st[i]][rnd[i]];
      }
      cout << bp * m[p] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// 32
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int main() {
  bool ns = false;
  for (int n; cin >> n, n;) {
    const char *s = "SCHD";
    int b[128][13];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 13; j++) {
        cin >> b[s[i]][j];
      }
    }
    int m[10] = {};
    for (int i = 0; i < 9; i++) {
      cin >> m[i + 1];
    }
    if (ns) {
      cout << endl;
    }
    ns = true;
    while (n--) {
      const char *r = "A23456789TJQK";
      int apr[13] = {}, aps[128] = {};
      char rn[5], st[5];
      int rnd[5];
      for (int i = 0; i < 5; i++) {
        cin >> rn[i] >> st[i];
        rnd[i] = strchr(r, rn[i]) - r;
        apr[rnd[i]]++;
        aps[st[i]]++;
      }
      int p;
      p = count(apr, apr + 13, 2);
      int tf = *max_element(apr, apr + 13);
      if (tf == 3) {
        p = 3;
      }
      bool str = false;
      int sp;
      for (int i = 0; i < 13 - 3; i++) {
        int h = true;
        for (int j = 0; j < 5; j++) {
          h &= apr[(i + j) % 13];
        }
        if (h) {
          str = true;
          p = 4;
          sp = i;
        }
      }
      bool fl = find(aps, aps + 128, 5) != aps + 128;
      if (fl) {
        p = 5;
      }
      if (find(apr, apr + 13, 2) != apr + 13 &&
          find(apr, apr + 13, 3) != apr + 13) {
        p = 6;
      }
      if (tf == 4) {
        p = 7;
      }
      if (str && fl) {
        p = 8 + (sp == 9);
      }
      int bp = 0;
      for (int i = 0; i < 5; i++) {
        bp += b[st[i]][rnd[i]];
      }
      cout << bp * p << endl;
    }
  }
  return 0;
}