Problem p03974 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct Trie {
  int ch[26];
  int sz[26];
  int ex;
  Trie() { clear(); }
  void clear() {
    REP(i, 26) ch[i] = -1;
    REP(i, 26) sz[i] = 0;
    ex = 0;
  }
};

const int MAX_N = 112345;
const int MAX_C = 2123456;
Trie trie[MAX_C];
int trie_sz = 0;

void add_trie(const int id, const string &s, const int pos) {
  if (pos == (int)s.size()) {
    trie[id].ex = 1;
    return;
  }
  int c = s[pos] - 'a';
  if (trie[id].ch[c] == -1) {
    trie[id].ch[c] = trie_sz++;
  }
  trie[id].sz[c]++;
  add_trie(trie[id].ch[c], s, pos + 1);
}

int prefix[MAX_N];
int comp[MAX_N][26][26];

void ready(const int id, const string &s, const int pos, const int k) {

  if (pos == (int)s.size()) {
    return;
  }

  prefix[k] += trie[id].ex;

  int c = s[pos] - 'a';
  REP(d, 26) if (c != d) { comp[k][c][d] += trie[id].sz[d]; }

  ready(trie[id].ch[c], s, pos + 1, k);
  return;
}

int main2() {

  int N = nextLong();
  CLR(prefix, 0);
  CLR(comp, 0);

  const int root = 0;
  trie_sz++;

  vector<string> S;
  REP(i, N) {
    string s;
    cin >> s;
    add_trie(root, s, 0);
    S.push_back(s);
  }

  for (int k = 0; k < N; k++) {
    ready(root, S[k], 0, k);
  }

  int Q = nextLong();
  while (Q--) {
    int k = nextLong() - 1;
    string p;
    cin >> p;

    vector<int> ord(26);
    REP(i, 26) ord[p[i] - 'a'] = i;

    int ans = 1 + prefix[k];
    REP(c, 26) REP(d, 26) if (ord[c] > ord[d]) { ans += comp[k][c][d]; }
    cout << ans << '\n';
  }

  REP(i, trie_sz) trie[i].clear();
  trie_sz = 0;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct Trie {
  int ch[26];
  int sz[26];
  int ex;
  Trie() { clear(); }
  void clear() {
    REP(i, 26) ch[i] = -1;
    REP(i, 26) sz[i] = 0;
    ex = 0;
  }
};

const int MAX_N = 112345;
const int MAX_C = 812345;
Trie trie[MAX_C];
int trie_sz = 0;

void add_trie(const int id, const string &s, const int pos) {
  if (pos == (int)s.size()) {
    trie[id].ex = 1;
    return;
  }
  int c = s[pos] - 'a';
  if (trie[id].ch[c] == -1) {
    trie[id].ch[c] = trie_sz++;
  }
  trie[id].sz[c]++;
  add_trie(trie[id].ch[c], s, pos + 1);
}

int prefix[MAX_N];
int comp[MAX_N][26][26];

void ready(const int id, const string &s, const int pos, const int k) {

  if (pos == (int)s.size()) {
    return;
  }

  prefix[k] += trie[id].ex;

  int c = s[pos] - 'a';
  REP(d, 26) if (c != d) { comp[k][c][d] += trie[id].sz[d]; }

  ready(trie[id].ch[c], s, pos + 1, k);
  return;
}

int main2() {

  int N = nextLong();
  CLR(prefix, 0);
  CLR(comp, 0);

  const int root = 0;
  trie_sz++;

  vector<string> S;
  REP(i, N) {
    string s;
    cin >> s;
    add_trie(root, s, 0);
    S.push_back(s);
  }

  for (int k = 0; k < N; k++) {
    ready(root, S[k], 0, k);
  }

  int Q = nextLong();
  while (Q--) {
    int k = nextLong() - 1;
    string p;
    cin >> p;

    vector<int> ord(26);
    REP(i, 26) ord[p[i] - 'a'] = i;

    int ans = 1 + prefix[k];
    REP(c, 26) REP(d, 26) if (ord[c] > ord[d]) { ans += comp[k][c][d]; }
    cout << ans << '\n';
  }

  REP(i, trie_sz) trie[i].clear();
  trie_sz = 0;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}