Problem p03974 (C++)

Buggy submission (status: Runtime Error):
/* ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄▄            ▄            ▄▄▄▄▄▄▄▄▄▄▄  ▄         ▄
▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌          ▐░▌
▐░░░░░░░░░░░▌▐░▌       ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌          ▐░▌          ▐░█▀▀▀▀▀▀▀█░▌▐░▌       ▐░▌
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ▐░▌          ▐░▌       ▐░▌▐░▌ ▐░▌ ▐░▌
▐░▌▐░▌       ▐░▌     ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌
▐░▌▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌   ▄   ▐░▌     ▐░▌ ▄▄▄▄▄▄▄▄ ▐░▌
▐░▌▐░▌       ▐░▌ ▐░░░░░░░░░░░▌▐░▌       ▐░▌▐░▌          ▐░▌          ▐░▌ ▐░▌▐░▌
▐░▌  ▐░▌     ▐░▌▐░░░░░░░░▌▐░▌       ▐░▌▐░▌       ▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌▐░▌ ▐░▌
▐░▌       ▐░▌▐░▌ ▐░▌░▌ ▐░▌     ▐░▌ ▀▀▀▀▀▀█░▌▐░▌       ▐░▌▐░▌       ▐░▌ ▐░▌ ▐░▌
▐░▌▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌▐░▌ ▐░▌▐░▌     ▐░▌       ▐░▌▐░▌
▐░▌▐░▌       ▐░▌ ▐░▌          ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄
▐░█▄▄▄▄▄▄▄█░▌▐░▌░▌   ▐░▐░▌     ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌ ▐░▌
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌     ▐░░▌
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▀            ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀       ▀▀       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀
*/

// #pragma optimization_level 3

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define ti tuple<int, int, int>
#define vi vector<int>
#define vpi vector<pii>
#define vld vector<ld>
#define try tr
#define left left11
#define right right11
#define lb lower_bound
#define ub upper_bound
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define pf push_front
#define sqr(a) ((a) * (a))
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define x first
#define y second

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

clock_t START;

inline ld TIME() { return (ld)(clock() - START) / CLOCKS_PER_SEC; }

void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}

ld TL = 2.0;
int calls = 0;

inline bool IS() {
  if (++calls == 1000) {
    calls = 0;
    if (TL - TIME() < 0.1)
      return true;
  }
  return false;
}

template <typename T1, typename T2> inline bool amin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> inline bool amax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v)
    os << u << ' ';
  return os;
}

int hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
    mod = hash_mod[rnd() % 4];
int hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];

int binpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p % 2)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p /= 2;
  }
  return res;
}

const int N = 1e5 + 7, NS = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7,
          SM = 1e2 + 7;
const ld eps = 1e-9;
struct nd {
  int go[26];
  int cnt = 0;
  int term = 0;
} t[N];
int sz = 1;
void add(string &s) {
  int v = 0;
  for (auto u : s) {
    int c = (u - 'a');
    t[v].cnt++;
    //   cout << v << ' ' << u << endl;
    if (!t[v].go[c])
      t[v].go[c] = sz++;
    v = t[v].go[c];
  }
  t[v].cnt++;
  t[v].term++;
}
int cnt[N][26][26], res[N];
void try(string &s, int i) {
  int v = 0;
  string l = "";
  for (auto u : s) {
    l += u;
    int c = (u - 'a');
    for (int c1 = 0; c1 < 26; c1++) {
      if (t[v].go[c1]) {
        cnt[i][c][c1] += t[t[v].go[c1]].cnt;
      }
    }
    v = t[v].go[c];
    res[i] += t[v].term;
  }
}
string s[N];
void solve() {
  int n, q;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    // cout << s[i] << endl;
    add(s[i]);
    //  cout << endl;
  }
  for (int i = 0; i < n; i++)
    try(s[i], i);
  cin >> q;
  while (q--) {
    int k;
    string l;
    cin >> k >> l;
    k--;
    vi pos(26);
    for (int i = 0; i < 26; i++) {
      int c = (l[i] - 'a');
      pos[c] = i;
    }
    int ans = res[k];
    // cout << cnt[k][1][0] << endl;
    for (int c = 0; c < 26; c++) {
      for (int c1 = 0; c1 < 26; c1++) {
        if (pos[c] > pos[c1])
          ans += cnt[k][c][c1];
      }
    }
    cout << ans << endl;
  }
}

signed main() { fast solve(); }

Fixed submission:
/* ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄▄            ▄            ▄▄▄▄▄▄▄▄▄▄▄  ▄         ▄
▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌          ▐░▌
▐░░░░░░░░░░░▌▐░▌       ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌          ▐░▌          ▐░█▀▀▀▀▀▀▀█░▌▐░▌       ▐░▌
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ▐░▌          ▐░▌       ▐░▌▐░▌ ▐░▌ ▐░▌
▐░▌▐░▌       ▐░▌     ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌
▐░▌▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌   ▄   ▐░▌     ▐░▌ ▄▄▄▄▄▄▄▄ ▐░▌
▐░▌▐░▌       ▐░▌ ▐░░░░░░░░░░░▌▐░▌       ▐░▌▐░▌          ▐░▌          ▐░▌ ▐░▌▐░▌
▐░▌  ▐░▌     ▐░▌▐░░░░░░░░▌▐░▌       ▐░▌▐░▌       ▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌▐░▌ ▐░▌
▐░▌       ▐░▌▐░▌ ▐░▌░▌ ▐░▌     ▐░▌ ▀▀▀▀▀▀█░▌▐░▌       ▐░▌▐░▌       ▐░▌ ▐░▌ ▐░▌
▐░▌▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌▐░▌ ▐░▌▐░▌     ▐░▌       ▐░▌▐░▌
▐░▌▐░▌       ▐░▌ ▐░▌          ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄
▐░█▄▄▄▄▄▄▄█░▌▐░▌░▌   ▐░▐░▌     ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌ ▐░▌
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌     ▐░░▌
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▀            ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀       ▀▀       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀
*/

// #pragma optimization_level 3

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define ti tuple<int, int, int>
#define vi vector<int>
#define vpi vector<pii>
#define vld vector<ld>
#define try tr
#define left left11
#define right right11
#define lb lower_bound
#define ub upper_bound
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define pf push_front
#define sqr(a) ((a) * (a))
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define x first
#define y second

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

clock_t START;

inline ld TIME() { return (ld)(clock() - START) / CLOCKS_PER_SEC; }

void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}

ld TL = 2.0;
int calls = 0;

inline bool IS() {
  if (++calls == 1000) {
    calls = 0;
    if (TL - TIME() < 0.1)
      return true;
  }
  return false;
}

template <typename T1, typename T2> inline bool amin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> inline bool amax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v)
    os << u << ' ';
  return os;
}

int hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
    mod = hash_mod[rnd() % 4];
int hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];

int binpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p % 2)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p /= 2;
  }
  return res;
}

const int N = 5e5 + 7, NS = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7,
          SM = 1e2 + 7;
const ld eps = 1e-9;
struct nd {
  int go[26];
  int cnt = 0;
  int term = 0;
} t[N];
int sz = 1;
void add(string &s) {
  int v = 0;
  for (auto u : s) {
    int c = (u - 'a');
    t[v].cnt++;
    //   cout << v << ' ' << u << endl;
    if (!t[v].go[c])
      t[v].go[c] = sz++;
    v = t[v].go[c];
  }
  t[v].cnt++;
  t[v].term++;
}
int cnt[N][26][26], res[N];
void try(string &s, int i) {
  int v = 0;
  string l = "";
  for (auto u : s) {
    l += u;
    int c = (u - 'a');
    for (int c1 = 0; c1 < 26; c1++) {
      if (t[v].go[c1]) {
        cnt[i][c][c1] += t[t[v].go[c1]].cnt;
      }
    }
    v = t[v].go[c];
    res[i] += t[v].term;
  }
}
string s[N];
void solve() {
  int n, q;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    // cout << s[i] << endl;
    add(s[i]);
    //  cout << endl;
  }
  for (int i = 0; i < n; i++)
    try(s[i], i);
  cin >> q;
  while (q--) {
    int k;
    string l;
    cin >> k >> l;
    k--;
    vi pos(26);
    for (int i = 0; i < 26; i++) {
      int c = (l[i] - 'a');
      pos[c] = i;
    }
    int ans = res[k];
    // cout << cnt[k][1][0] << endl;
    for (int c = 0; c < 26; c++) {
      for (int c1 = 0; c1 < 26; c1++) {
        if (pos[c] > pos[c1])
          ans += cnt[k][c][c1];
      }
    }
    cout << ans << endl;
  }
}

signed main() { fast solve(); }