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

#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi>
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii>
#define vpll vector<pll>
#define pb push_back
#define loop(i, s, l, j) for (int i = s; i < l; i += j)
#define rloop(i, s, l, j) for (int i = s; i >= l; i -= j)
#define rep(i, n) loop(i, 0, n, 1)
#define rrep(i, n) rloop(i, n - 1, 0, 1)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ft first
#define sd second
#define mit mii::iterator
#define vit vi::iterator
#define all(a) a.begin(), a.end()
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define inf 10000000000000000
#define limit 200005
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base, int exp) {
  int ans = 1;
  while (exp > 0) {
    if (exp & 1)
      ans = (1LL * ans * base) % mod;
    exp = exp >> 1;
    base = (1LL * base * base) % mod;
  }
  return ans;
}
/**********************************************************************************************************************************************/

struct TrieNode {
  vector<TrieNode *> ptr;
  vi fre;
  TrieNode() {
    ptr.resize(26);
    rep(i, 26) ptr[i] = NULL;
    fre.resize(26, 0);
  }
};
void Insert(TrieNode *root, str s) {
  int n = s.length();

  rloop(i, n - 1, 1, 1) {
    int c = s[i] - 'a';
    if (root->ptr[c])
      root = root->ptr[c];
    else {
      root->ptr[c] = new TrieNode;
      root = root->ptr[c];
    }
  }
  root->fre[s[0] - 'a']++;
}
void input() {
  int n;
  cin >> n;

  vs s(n);
  TrieNode *root = new TrieNode;
  rep(i, n) {
    cin >> s[i];
    s[i] += 'a';
    Insert(root, s[i]);
  }

  lli ans = 0;
  rep(i, n) {
    TrieNode *tmp = root;
    vi fres(26, 0);
    int m = s[i].length();
    rep(j, m) fres[s[i][j] - 'a']++;

    rloop(j, m - 1, 1, 1) {
      tmp = tmp->ptr[s[i][j] - 'a'];
      fres[s[i][j] - 'a']--;
      rep(k, 26) if (tmp->fre[k] && fres[k]) {
        if (j == 1)
          ans += max(0, tmp->fre[k] - 1);
        else
          ans += tmp->fre[k];
      }
    }
  }
  cout << ans << '\\n';
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fastio();
  int q = 1;
  // cin >> q;
  while (q--)
    input();
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi>
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii>
#define vpll vector<pll>
#define pb push_back
#define loop(i, s, l, j) for (int i = s; i < l; i += j)
#define rloop(i, s, l, j) for (int i = s; i >= l; i -= j)
#define rep(i, n) loop(i, 0, n, 1)
#define rrep(i, n) rloop(i, n - 1, 0, 1)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ft first
#define sd second
#define mit mii::iterator
#define vit vi::iterator
#define all(a) a.begin(), a.end()
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define inf 10000000000000000
#define limit 200005
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base, int exp) {
  int ans = 1;
  while (exp > 0) {
    if (exp & 1)
      ans = (1LL * ans * base) % mod;
    exp = exp >> 1;
    base = (1LL * base * base) % mod;
  }
  return ans;
}
/**********************************************************************************************************************************************/

struct TrieNode {
  vector<TrieNode *> ptr;
  vi fre;
  TrieNode() {
    ptr.resize(26);
    rep(i, 26) ptr[i] = NULL;
    fre.resize(26, 0);
  }
};
TrieNode *Insert(TrieNode *root, str s) {
  int n = s.length();

  rloop(i, n - 1, 1, 1) {
    int c = s[i] - 'a';
    if (root->ptr[c])
      root = root->ptr[c];
    else {
      root->ptr[c] = new TrieNode;
      root = root->ptr[c];
    }
  }
  root->fre[s[0] - 'a']++;
}
void input() {
  int n;
  cin >> n;

  vs s(n);
  TrieNode *root = new TrieNode;
  rep(i, n) {
    cin >> s[i];
    s[i] += 'a';
    Insert(root, s[i]);
  }

  lli ans = 0;
  rep(i, n) {
    TrieNode *tmp = root;
    vi fres(26, 0);
    int m = s[i].length();
    rep(j, m) fres[s[i][j] - 'a']++;

    rloop(j, m - 1, 1, 1) {
      tmp = tmp->ptr[s[i][j] - 'a'];
      fres[s[i][j] - 'a']--;
      rep(k, 26) if (tmp->fre[k] && fres[k]) ans += tmp->fre[k];
    }
  }
  cout << ans / 2 << '\\n';
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fastio();
  int q = 1;
  // cin >> q;
  while (q--)
    input();
}