Problem p03974 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long int;
const int AL = 26 + 1;
const int MAXN = 10;
struct trienode {
  vector<trienode *> nodes;
  int count = 0;
  trienode() { nodes.resize(AL, NULL); }
  trienode *&operator[](int ch) { return nodes[ch]; }
};
int dp[MAXN][AL][AL];
struct trie {
  trienode begin;
  void get(const string &s, int idx) {
    trienode *currnode = &begin;
    for (int i = 0; i < s.size(); i++) {
      int ch = s[i] - 'a' + 1;
      if ((*currnode)[ch] == NULL) {
        (*currnode)[ch] = new trienode();
      }
      for (int i = 0; i < AL; i++) {
        if ((*currnode)[i] == NULL)
          continue;
        dp[idx][ch][i] += (*currnode)[i]->count;
      }
      currnode = (*currnode)[ch];
    }
  }
  void add(const string &s) {
    trienode *currnode = &begin;
    ++currnode->count;
    for (int i = 0; i < s.size(); i++) {
      int ch = s[i] - 'a' + 1;
      if ((*currnode)[ch] == NULL) {
        (*currnode)[ch] = new trienode();
      }
      currnode = (*currnode)[ch];
      ++currnode->count;
    }
  }
};
void solve() {
  int n;
  cin >> n;
  trie solver;
  vector<string> seq(n);
  for (int i = 0; i < n; i++) {
    cin >> seq[i];
    seq[i].pb('a' - 1);
    solver.add(seq[i]);
  }
  for (int i = 0; i < n; i++) {
    solver.get(seq[i], i);
  }
  int q;
  cin >> q;
  while (q--) {
    int k;
    string order;
    vector<int> idx(AL);
    cin >> k >> order;
    int ans = 0;
    for (int i = 0; i < AL - 1; i++) {
      idx[order[i] - 'a' + 1] = i + 1;
    }
    for (int i = 0; i < AL; i++) {
      for (int j = 0; j < AL; j++) {
        if (idx[i] <= idx[j]) {
          continue;
        }
        ans += dp[k - 1][i][j];
      }
    }
    cout << ans + 1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long int;
const int AL = 26 + 1;
const int MAXN = 100'000;
struct trienode {
  vector<trienode *> nodes;
  int count = 0;
  trienode() { nodes.resize(AL, NULL); }
  trienode *&operator[](int ch) { return nodes[ch]; }
};
int dp[MAXN][AL][AL];
struct trie {
  trienode begin;
  void get(const string &s, int idx) {
    trienode *currnode = &begin;
    for (int i = 0; i < s.size(); i++) {
      int ch = s[i] - 'a' + 1;
      if ((*currnode)[ch] == NULL) {
        (*currnode)[ch] = new trienode();
      }
      for (int i = 0; i < AL; i++) {
        if ((*currnode)[i] == NULL)
          continue;
        dp[idx][ch][i] += (*currnode)[i]->count;
      }
      currnode = (*currnode)[ch];
    }
  }
  void add(const string &s) {
    trienode *currnode = &begin;
    ++currnode->count;
    for (int i = 0; i < s.size(); i++) {
      int ch = s[i] - 'a' + 1;
      if ((*currnode)[ch] == NULL) {
        (*currnode)[ch] = new trienode();
      }
      currnode = (*currnode)[ch];
      ++currnode->count;
    }
  }
};
void solve() {
  int n;
  cin >> n;
  trie solver;
  vector<string> seq(n);
  for (int i = 0; i < n; i++) {
    cin >> seq[i];
    seq[i].pb('a' - 1);
    solver.add(seq[i]);
  }
  for (int i = 0; i < n; i++) {
    solver.get(seq[i], i);
  }
  int q;
  cin >> q;
  while (q--) {
    int k;
    string order;
    vector<int> idx(AL);
    cin >> k >> order;
    int ans = 0;
    for (int i = 0; i < AL - 1; i++) {
      idx[order[i] - 'a' + 1] = i + 1;
    }
    for (int i = 0; i < AL; i++) {
      for (int j = 0; j < AL; j++) {
        if (idx[i] <= idx[j]) {
          continue;
        }
        ans += dp[k - 1][i][j];
      }
    }
    cout << ans + 1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}