Problem p03974 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, string> pip;
typedef vector<pip> vip;
const int inf = 2 * 1e9 + 1;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Trie {
private:
  const int MAX = 100;
  int ID = 1;
  vvi date;
  vi u, v;

public:
  Trie() {
    date = vvi(MAX, vi(26, -1));
    u = v = vi(MAX);
  }
  void Update(string s) {
    int S = s.size(), I = 0;
    for (int i = 0; i < S; i++) {
      int c = s[i] - 'a';
      if (date[I][c] == -1)
        date[I][c] = ID++;
      I = date[I][c];
      u[I]++;
    }
    v[I]++;
  }
  int Find(string s) {
    int S = s.size(), I = 0, res = -1;
    for (int i = 0; i < S; i++) {
      I = date[I][s[i] - 'a'];
      res += v[I];
    }
    return res;
  }
  vvi solve(string s) {
    vvi a(26, vi(26));
    int S = s.size(), I = 0;
    for (int i = 0; i < S; i++) {
      int c = s[i] - 'a';
      for (int j = 0; j < 26; j++)
        if (j != c && date[I][j] != -1)
          a[j][c] += u[date[I][j]];
      I = date[I][c];
    }
    return a;
  }
};

int n, q;
vs a, t;

int main() {
  cin >> n;
  a = vs(n);
  vi b(n);
  vvi c(26);
  Trie trie;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    trie.Update(a[i]);
  }
  for (int i = 0; i < n; i++)
    b[i] = trie.Find(a[i]);
  cin >> q;
  t = vs(q);
  vp d(q);
  vi res(q);
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k >> t[i];
    d[i] = {k - 1, i};
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < q; i++) {
    int k = d[i].first, I = d[i].second;
    string s = t[I];
    if (!i || k != d[i - 1].first)
      c = trie.solve(a[k]);
    res[I] = b[k];
    for (int j = 0; j < 26; j++)
      for (int l = 0; l < j; l++)
        res[I] += c[s[l] - 'a'][s[j] - 'a'];
  }
  for (int i = 0; i < q; i++)
    cout << res[i] + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, string> pip;
typedef vector<pip> vip;
const int inf = 2 * 1e9 + 1;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Trie {
private:
  const int MAX = 400005;
  int ID = 1;
  vvi date;
  vi u, v;

public:
  Trie() {
    date = vvi(MAX, vi(26, -1));
    u = v = vi(MAX);
  }
  void Update(string s) {
    int S = s.size(), I = 0;
    for (int i = 0; i < S; i++) {
      int c = s[i] - 'a';
      if (date[I][c] == -1)
        date[I][c] = ID++;
      I = date[I][c];
      u[I]++;
    }
    v[I]++;
  }
  int Find(string s) {
    int S = s.size(), I = 0, res = -1;
    for (int i = 0; i < S; i++) {
      I = date[I][s[i] - 'a'];
      res += v[I];
    }
    return res;
  }
  vvi solve(string s) {
    vvi a(26, vi(26));
    int S = s.size(), I = 0;
    for (int i = 0; i < S; i++) {
      int c = s[i] - 'a';
      for (int j = 0; j < 26; j++)
        if (j != c && date[I][j] != -1)
          a[j][c] += u[date[I][j]];
      I = date[I][c];
    }
    return a;
  }
};

int n, q;
vs a, t;

int main() {
  cin >> n;
  a = vs(n);
  vi b(n);
  vvi c(26);
  Trie trie;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    trie.Update(a[i]);
  }
  for (int i = 0; i < n; i++)
    b[i] = trie.Find(a[i]);
  cin >> q;
  t = vs(q);
  vp d(q);
  vi res(q);
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k >> t[i];
    d[i] = {k - 1, i};
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < q; i++) {
    int k = d[i].first, I = d[i].second;
    string s = t[I];
    if (!i || k != d[i - 1].first)
      c = trie.solve(a[k]);
    res[I] = b[k];
    for (int j = 0; j < 26; j++)
      for (int l = 0; l < j; l++)
        res[I] += c[s[l] - 'a'][s[j] - 'a'];
  }
  for (int i = 0; i < q; i++)
    cout << res[i] + 1 << endl;
}