Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 400
#define offset 200
#define ptr nodo *
struct nodo {
  map<char, ptr> hijos;
  int mapa[30];
};
nodo basura[300000];
int cnodo = 0;
ptr getnodo() {
  memset(basura[cnodo].mapa, 0, sizeof basura[cnodo].mapa);
  return &basura[cnodo++];
}
int mapa[tam];
void add(ptr root) {
  forr(i, 0, 27) {
    if (mapa[i] != -1)
      root->mapa[i]++;
  }
}

void insertar(ptr root, string &txt) {
  //<<txt<<endl;
  forr(i, 0, txt.size()) {
    //		print();

    //<<root<<":";
    add(root);
    if (mapa[txt[i]] == i)
      mapa[txt[i]] = -1;
    if (root->hijos.find(txt[i]) == root->hijos.end())
      root->hijos[txt[i]] = getnodo();

    root = root->hijos[txt[i]];
  }
  //<<endl<<endl;
}
ll tot = 0;
void query(ptr root, string &txt) {
  forr(i, 0, txt.size() - 1) { root = root->hijos[txt[i]]; }
  tot += max(0, root->mapa[txt.back()] - 1);
  //<<int(txt.back())<<" "<<root->mapa[txt.back()]<<" ";

  //<<tot<<"endl\n";
}
int main() {
  int n;
  cin >> n;
  vector<string> lst;
  ptr root = getnodo();
  forr(i, 0, n) {
    string txt;
    cin >> txt;

    forr(j, 0, txt.size()) txt[j] -= 'a';

    reverse(all(txt));
    memset(mapa, -1, sizeof mapa);
    forr(j, 0, txt.size()) mapa[txt[j]] = j;
    insertar(root, txt);
    lst.pb(txt);
  }
  for (string txt : lst) {
    query(root, txt);
  }
  cout << tot << endl;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 400
#define offset 200
#define ptr nodo *
struct nodo {
  map<char, ptr> hijos;
  int mapa[30];
};
nodo basura[1200000];
int cnodo = 0;
ptr getnodo() {
  memset(basura[cnodo].mapa, 0, sizeof basura[cnodo].mapa);
  return &basura[cnodo++];
}
int mapa[tam];
void add(ptr root) {
  forr(i, 0, 27) {
    if (mapa[i] != -1)
      root->mapa[i]++;
  }
}

void insertar(ptr root, string &txt) {
  //<<txt<<endl;
  forr(i, 0, txt.size()) {
    //		print();

    //<<root<<":";
    add(root);
    if (mapa[txt[i]] == i)
      mapa[txt[i]] = -1;
    if (root->hijos.find(txt[i]) == root->hijos.end())
      root->hijos[txt[i]] = getnodo();

    root = root->hijos[txt[i]];
  }
  //<<endl<<endl;
}
ll tot = 0;
void query(ptr root, string &txt) {
  forr(i, 0, txt.size() - 1) { root = root->hijos[txt[i]]; }
  tot += max(0, root->mapa[txt.back()] - 1);
  //<<int(txt.back())<<" "<<root->mapa[txt.back()]<<" ";

  //<<tot<<"endl\n";
}
int main() {
  int n;
  cin >> n;
  vector<string> lst;
  ptr root = getnodo();
  forr(i, 0, n) {
    string txt;
    cin >> txt;

    forr(j, 0, txt.size()) txt[j] -= 'a';

    reverse(all(txt));
    memset(mapa, -1, sizeof mapa);
    forr(j, 0, txt.size()) mapa[txt[j]] = j;
    insertar(root, txt);
    lst.pb(txt);
  }
  for (string txt : lst) {
    query(root, txt);
  }
  cout << tot << endl;
}