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

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
  a = 0;
  char x = getchar();
  bool f = 0;
  for (; x < '0' || x > '9'; x = getchar())
    f |= x == '-';
  for (; x >= '0' && x <= '9'; x = getchar())
    a = (a << 3) + (a << 1) + x - '0';
  if (f)
    a = -a;
  return a;
}
using namespace std;

const int N = 2e5 + 5;

typedef bitset<26> bst;

struct tp {
  tp *child[26];
  int cnt[26];
  tp() {
    for (int i = 0; i < 26; ++i) {
      child[i] = 0;
      cnt[i] = 0;
    }
  }
} *root;

int n;
char s[10 * N];

bst update(int u, tp *cur) {
  if (u == 0) {
    bst c;
    return c;
  }
  int a = s[u] - 'a';
  if (!cur->child[a])
    cur->child[a] = new tp();
  bst b = update(u - 1, cur->child[a]);
  if (!b[a])
    b[a] = 1;
  for (int i = 0; i < 26; ++i) {
    cur->cnt[i] += b[i];
  }
  return b;
}

int query(tp *cur, int len) {
  int a = s[len] - 'a';
  if (!cur) {
    return 0;
  }
  if (len == 1) {
    return cur->cnt[a];
  }
  return query(cur->child[a], len - 1);
}

string str[N];

int main() {
  root = new tp();
  read(n);
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> str[i];
    int len = str[i].size();
    for (int j = 0; j < len; ++j) {
      s[j + 1] = str[i][j];
    }
    update(len, root);
  }
  for (int i = 1; i <= n; ++i) {
    int len = str[i].size();
    for (int j = 0; j < len; ++j) {
      s[j + 1] = str[i][j];
    }
    ans += query(root, len);
  }
  printf("%lld\\n", ans - n);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
  a = 0;
  char x = getchar();
  bool f = 0;
  for (; x < '0' || x > '9'; x = getchar())
    f |= x == '-';
  for (; x >= '0' && x <= '9'; x = getchar())
    a = (a << 3) + (a << 1) + x - '0';
  if (f)
    a = -a;
  return a;
}
using namespace std;

const int N = 2e5 + 5;

typedef bitset<26> bst;

struct tp {
  tp *child[26];
  int cnt[26];
  tp() {
    for (int i = 0; i < 25; ++i) {
      child[i] = 0;
      cnt[i] = 0;
    }
  }
} *root;

int n;
char s[10 * N];

bst update(int u, tp *cur) {
  if (u == 0) {
    bst c;
    return c;
  }
  int a = s[u] - 'a';
  if (!cur->child[a])
    cur->child[a] = new tp();
  bst b = update(u - 1, cur->child[a]);
  if (!b[a])
    b[a] = 1;
  for (int i = 0; i < 26; ++i) {
    cur->cnt[i] += b[i];
  }
  return b;
}

int query(tp *cur, int len) {
  int a = s[len] - 'a';
  if (!cur) {
    return 0;
  }
  if (len == 1) {
    return cur->cnt[a];
  }
  return query(cur->child[a], len - 1);
}

string str[N];

int main() {
  root = new tp();
  read(n);
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> str[i];
    for (int j = 0; j < str[i].size(); ++j) {
      s[j + 1] = str[i][j];
    }
    int len = str[i].size();
    update(len, root);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < str[i].size(); ++j) {
      s[j + 1] = str[i][j];
    }
    int len = str[i].size();
    ans += query(root, len);
  }
  printf("%lld\\n", ans - n);
  return 0;
}