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

/*
Problem :
Algorithm :
Status :
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define DEBUG                                                                  \\
  printf("Passing Line %d in Function [%s].\\n", __LINE__, __FUNCTION__);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 5;

int n;
int cnt[30], flag[30];
ll ans;
string s[MAXN];

namespace Trie {
int tot = 1;
int trie[MAXN][26], d[MAXN][26];

void Insert(string s) {
  int len = s.length();
  int now = 1;
  int pre[26];
  memset(pre, 0, sizeof(pre));
  for (int i = 0; i < len; i++) {
    int ch = s[i] - 'a';
    if (!trie[now][ch])
      trie[now][ch] = ++tot;
    now = trie[now][ch];
    d[now][ch] += 1;
    if (pre[ch])
      d[pre[ch]][ch] -= 1;
    pre[ch] = now;
  }
}

void dfs(int u) {
  for (int i = 0; i < 26; i++) {
    int v = trie[u][i];
    if (v) {
      dfs(v);
      for (int j = 0; j < 26; j++)
        d[u][j] += d[v][j];
    }
  }
  // cout << u << " : " << d[u][0] << " " << d[u][1] << endl;
}

int Query(string s) {
  int len = s.length();
  int now = 1;
  for (int i = 0; i < len - 1; i++) {
    int ch = s[i] - 'a';
    now = trie[now][ch];
  }
  int res = 0;
  for (int i = 0; i < 26; i++) {
    if (trie[now][i]) {
      // cout << trie[now][i] << " " << d[trie[now][i]][s.back() - 'a'] << endl;
      res += d[trie[now][i]][s.back() - 'a'];
    }
  }
  return res;
}
} // namespace Trie

int main() {
  // freopen("data.in","r",stdin);
  // freopen("data.out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    Trie::Insert(s[i]);
  }
  Trie::dfs(1);
  for (int i = 1; i <= n; i++) {
    // cout << s[i] << " : " << Trie::Query(s[i]) - 1 << endl;
    ans += Trie::Query(s[i]) - 1;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

/*
Problem :
Algorithm :
Status :
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define DEBUG                                                                  \\
  printf("Passing Line %d in Function [%s].\\n", __LINE__, __FUNCTION__);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;

int n;
int cnt[30], flag[30];
ll ans;
string s[MAXN];

namespace Trie {
int tot = 1;
int trie[MAXN][26], size[MAXN], end[MAXN];

void Insert(string s) {
  int len = s.length();
  int now = 1;
  for (int i = 0; i < len; i++) {
    int ch = s[i] - 'a';
    if (!trie[now][ch])
      trie[now][ch] = ++tot;
    now = trie[now][ch];
  }
  end[now] = 1;
}

void dfs(int u, int fa) {
  size[u] = end[u];
  for (int i = 0; i < 26; i++) {
    int v = trie[u][i];
    if (v) {
      dfs(v, u);
      size[u] += size[v];
    }
  }
}

int Query(string s) {
  int len = s.length();
  int now = 1;
  for (int i = 0; i < len - 1; i++) {
    int ch = s[i] - 'a';
    now = trie[now][ch];
  }
  return size[now];
}
} // namespace Trie

int main() {
  // freopen("data.in","r",stdin);
  // freopen("data.out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    memset(flag, 0, sizeof(flag));
    for (int j = 0; j < (int)s[i].length(); j++)
      flag[s[i][j] - 'a'] = 1;
    for (int j = 0; j < 26; j++)
      cnt[j] += flag[j];
  }
  for (int i = 1; i <= n; i++) {
    if (s[i].length() == 1) {
      // cout << s[i] << " : " << cnt[s[i][0] - 'a'] << endl;
      ans += cnt[s[i][0] - 'a'] - 1;
      continue;
    }
    Trie::Insert(s[i]);
  }
  Trie::dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (s[i].length() == 1)
      continue;
    // cout << s[i] << " : " << Trie::Query(s[i]) - 1 << endl;
    ans += Trie::Query(s[i]) - 1;
  }
  cout << ans << endl;
  return 0;
}