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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

struct node {
  node *next[11];
  int m;
};

node gN[100 * 1000];
int cnt;

node *myalloc() {
  REP(i, 11) gN[cnt].next[i] = NULL;
  gN[cnt].m = 0;
  return &gN[cnt++];
}

void mknode(node *nd, char *c, int m) {
  if (*c == '\\0') {
    nd->m = m;
    return;
  }
  int nn = (*c == '*' ? 10 : (*c - '0'));
  node *tmp = nd->next[nn];
  if (!tmp)
    tmp = nd->next[nn] = myalloc();
  mknode(tmp, c + 1, m);
}

int check(node *nd, char *c) {
  if (!nd)
    return 0;
  if (*c == '\\0')
    return nd->m;
  // printf("check(%c)\\n",*c);
  return max(check(nd->next[*c - '0'], c + 1), check(nd->next[10], c + 1));
}

int main() {
  int n, m;
  while (scanf("%d%d ", &n, &m), n + m) {
    char buff[20];
    int c;
    node *root = myalloc();
    REP(i, n) {
      scanf("%s %d ", buff, &c);
      mknode(root, buff, c);
    }
    int ans = 0;
    REP(i, m) {
      scanf("%s", buff);
      // printf("%d (%s)\\n",check(root,buff),buff);
      ans += check(root, buff);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

struct node {
  node *next[11];
  int m;
};

node gN[100 * 100];
int cnt;

node *myalloc() {
  REP(i, 11) gN[cnt].next[i] = NULL;
  gN[cnt].m = 0;
  return &gN[cnt++];
}

void mknode(node *nd, char *c, int m) {
  if (*c == '\\0') {
    nd->m = m;
    return;
  }
  int nn = (*c == '*' ? 10 : (*c - '0'));
  node *tmp = nd->next[nn];
  if (!tmp)
    tmp = nd->next[nn] = myalloc();
  mknode(tmp, c + 1, m);
}

int check(node *nd, char *c) {
  if (!nd)
    return 0;
  if (*c == '\\0')
    return nd->m;
  // printf("check(%c)\\n",*c);
  return max(check(nd->next[*c - '0'], c + 1), check(nd->next[10], c + 1));
}

int main() {
  int n, m;
  while (scanf("%d%d ", &n, &m), n + m) {
    char buff[20];
    int c;
    node *root = myalloc();
    REP(i, n) {
      scanf("%s %d ", buff, &c);
      mknode(root, buff, c);
    }
    int ans = 0;
    REP(i, m) {
      scanf("%s", buff);
      // printf("%d (%s)\\n",check(root,buff),buff);
      ans += check(root, buff);
    }
    printf("%d\\n", ans);
  }
  return 0;
}