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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

const string table[] = {
    "abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! ",
};

string solve(string src) {
  if (src.size() % 2) {
    return "NA";
  }

  string res;

  for (int i = 0; i < src.size(); i += 2) {
    int idx1 = src[i] - '1', idx2 = src[i + 1] - '1';
    if (!(0 <= idx1 && idx1 < 6 && 0 <= idx2 && idx2 < 5)) {
      return "NA";
    }
    res += table[idx1][idx2];
  }

  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string str;
  while (cin >> str) {
    cout << solve(str) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

const string table[] = {
    "abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! ",
};

string solve(string src) {
  if (src.size() % 2) {
    return "NA";
  }

  string res;

  for (int i = 0; i < src.size(); i += 2) {
    int idx1 = src[i] - '1', idx2 = src[i + 1] - '1';
    if (6 <= idx1 || 5 <= idx2) {
      return "NA";
    }
    res += table[idx1][idx2];
  }

  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string str;
  while (cin >> str) {
    cout << solve(str) << endl;
  }

  return 0;
}