Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

class trie {
public:
  trie(size_t max_size) {
    size = 1; // only root
    next.assign(26, vector<int>(max_size, -1));
    end.assign(max_size, false);
    //
    count.assign(26, vector<int>(max_size, 0));
    //
  }

  bool search(string s) {
    int now = 0;
    for (char c : s) {
      if (next[c - 'a'][now] == -1)
        return false;
      now = next[c - 'a'][now];
    }
    return end[now];
  }

  void insert(string s) {
    int now = 0;
    vector<int> path;
    for (char c : s) {
      path.push_back(now);
      if (next[c - 'a'][now] == -1)
        next[c - 'a'][now] = size++;
      now = next[c - 'a'][now];
    }
    end[now] = true;

    int mask = 0;
    for (int i = s.length() - 1; i >= 0; --i) {
      mask |= 1 << (s[i] - 'a');
      for (int j = 0; j < 26; ++j)
        if (mask >> j & 1)
          count[j][path[i]]++;
    }
  }
  //
  int solve(string s, char x) {
    int now = 0;
    for (char c : s)
      now = next[c - 'a'][now];
    return count[x - 'a'][now];
  }
  //

private:
  vector<vector<int>> next;
  vector<bool> end;
  //
  vector<vector<int>> count;
  //
  int size;
};

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  trie tr(1000001);
  rep(i, 0, n) {
    cin >> a[i];
    reverse(a[i].begin(), a[i].end());
    tr.insert(a[i]);
  }
  int ans = 0;
  rep(i, 0, n) {
    char last = a[i].back();
    a[i].pop_back();
    ans += tr.solve(a[i], last) - 1;
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

class trie {
public:
  trie(size_t max_size) {
    size = 1; // only root
    next.assign(26, vector<int>(max_size, -1));
    end.assign(max_size, false);
    //
    count.assign(26, vector<int>(max_size, 0));
    //
  }

  bool search(string s) {
    int now = 0;
    for (char c : s) {
      if (next[c - 'a'][now] == -1)
        return false;
      now = next[c - 'a'][now];
    }
    return end[now];
  }

  void insert(string s) {
    int now = 0;
    vector<int> path;
    for (char c : s) {
      path.push_back(now);
      if (next[c - 'a'][now] == -1)
        next[c - 'a'][now] = size++;
      now = next[c - 'a'][now];
    }
    end[now] = true;

    int mask = 0;
    for (int i = s.length() - 1; i >= 0; --i) {
      mask |= 1 << (s[i] - 'a');
      for (int j = 0; j < 26; ++j)
        if (mask >> j & 1)
          count[j][path[i]]++;
    }
  }
  //
  int solve(string s, char x) {
    int now = 0;
    for (char c : s)
      now = next[c - 'a'][now];
    return count[x - 'a'][now];
  }
  //

private:
  vector<vector<int>> next;
  vector<bool> end;
  //
  vector<vector<int>> count;
  //
  int size;
};

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  trie tr(100);
  rep(i, 0, n) {
    cin >> a[i];
    reverse(a[i].begin(), a[i].end());
    tr.insert(a[i]);
  }
  int ans = 0;
  rep(i, 0, n) {
    char last = a[i].back();
    a[i].pop_back();
    ans += tr.solve(a[i], last) - 1;
  }
  cout << ans << '\\n';
  return 0;
}