Problem p03974 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s[100004];
int trie[400005][27];
int d[400005];
string p[100004];

int main() {
  int n;
  cin >> n;
  int m = 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    int c = 0;
    for (int j = 0; j < (int)s[i].size(); j++) {
      if (trie[c][s[i][j] - 'a'] == 0) {
        trie[c][s[i][j] - 'a'] = m++;
      }
      c = trie[c][s[i][j] - 'a'];
      d[c]++;
    }
    d[m]++;
    trie[c][26] = m++;
  }
  int q;
  cin >> q;
  vector<int> v[100005];
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k >> p[i];
    k--;
    v[k].push_back(i);
  }
  int ans[100002];
  fill(ans, ans + q, 1);
  for (int i = 0; i < n; i++) {
    if (v[i].size() == 0)
      continue;
    int r[27][27];
    for (int j = 0; j < 27; j++) {
      for (int k = 0; k < 27; k++)
        r[j][k] = 0;
    }
    int c = 0;
    for (int j = 0; j < (int)s[i].size(); j++) {
      for (int k = 0; k < 27; k++) {
        if (k == s[i][j] - 'a')
          continue;
        r[k][s[i][j] - 'a'] += d[trie[c][k]];
      }
      c = trie[c][s[i][j] - 'a'];
    }
    for (int k = 0; k < 26; k++) {
      r[k][26] += d[trie[c][k]];
    }
    for (int j : v[i]) {
      for (int l = 0; l < 26; l++)
        ans[j] += r[26][l];
      bool b[26]{0};
      for (int k = 0; k < 26; k++) {
        b[p[j][k] - 'a'] = true;
        for (int l = 0; l < 26; l++) {
          if (!b[l])
            ans[j] += r[p[j][k] - 'a'][l];
        }
      }
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s[100004];
int trie[500005][27];
int d[500005];
string p[100004];

int main() {
  int n;
  cin >> n;
  int m = 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    int c = 0;
    for (int j = 0; j < (int)s[i].size(); j++) {
      if (trie[c][s[i][j] - 'a'] == 0) {
        trie[c][s[i][j] - 'a'] = m++;
      }
      c = trie[c][s[i][j] - 'a'];
      d[c]++;
    }
    d[m]++;
    trie[c][26] = m++;
  }
  int q;
  cin >> q;
  vector<int> v[100005];
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k >> p[i];
    k--;
    v[k].push_back(i);
  }
  int ans[100002];
  fill(ans, ans + q, 1);
  for (int i = 0; i < n; i++) {
    if (v[i].size() == 0)
      continue;
    int r[27][27];
    for (int j = 0; j < 27; j++) {
      for (int k = 0; k < 27; k++)
        r[j][k] = 0;
    }
    int c = 0;
    for (int j = 0; j < (int)s[i].size(); j++) {
      for (int k = 0; k < 27; k++) {
        if (k == s[i][j] - 'a')
          continue;
        r[k][s[i][j] - 'a'] += d[trie[c][k]];
      }
      c = trie[c][s[i][j] - 'a'];
    }
    for (int k = 0; k < 26; k++) {
      r[k][26] += d[trie[c][k]];
    }
    for (int j : v[i]) {
      for (int l = 0; l < 26; l++)
        ans[j] += r[26][l];
      bool b[26]{0};
      for (int k = 0; k < 26; k++) {
        b[p[j][k] - 'a'] = true;
        for (int l = 0; l < 26; l++) {
          if (!b[l])
            ans[j] += r[p[j][k] - 'a'][l];
        }
      }
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}