Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef OJ
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define sz(x) ((int)x.size())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << (x) << '\n';                                                       \
    return 0;                                                                  \
  } while (0)

const char en = '\n';
const int MOD = 0;
const int MAXN = 0;
const int INF = 1e9;

struct stNode {
  vector<stNode *> child = vector<stNode *>(26, NULL);
  vector<int> has = vector<int>(26);

  void add(const string &s, const vector<int> &lst, int i) {
    if (i == sz(s))
      return;
    int c = s[i] - 'a';
    if (child[c] == NULL)
      child[c] = new stNode();
    fori(k, 26) {
      if (lst[k] >= i) {
        has[k]++;
      }
    }
    child[c]->add(s, lst, i + 1);
  }

  int check(const string &s, int i) {
    int c = s[i] - 'a';
    if (i == sz(s) - 1) {
      return has[c];
    }
    if (child[c] == NULL)
      return false;
    return child[c]->check(s, i + 1);
  }
};

int solve() {
  int n;
  cin >> n;
  vector<string> v(n);
  map<string, int> m;
  fori(i, n) {
    cin >> v[i];
    reverse(all(v[i]));
    m[v[i]]++;
  }

  stNode *root1 = new stNode();
  LL ans = 0;
  fori(i, n) {
    string &s = v[i];
    ans += root1->check(s, 0);

    debug(s, ans);

    vector<int> lst(26, -1);
    fori(j, sz(s)) lst[s[j] - 'a'] = j;

    root1->add(s, lst, 0);
  }

  stNode *root2 = new stNode();
  ford(i, n - 1, 0) {
    string &s = v[i];
    ans += root2->check(s, 0);

    vector<int> lst(26, -1);
    fori(j, sz(s)) lst[s[j] - 'a'] = j;

    root2->add(s, lst, 0);
  }

  for (auto &it : m) {
    ans -= 1LL * it.se * (it.se - 1) / 2;
  }
  cout << ans << en;
}

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  int ntest = 1;
  // cin >> ntest;
  fori(test, ntest) {
    // cout << "Case #" << test+1 << ": ";
    solve();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef OJ
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define sz(x) ((int)x.size())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << (x) << '\n';                                                       \
    return 0;                                                                  \
  } while (0)

const char en = '\n';
const int MOD = 0;
const int MAXN = 0;
const int INF = 1e9;

struct stNode {
  vector<stNode *> child = vector<stNode *>(26, NULL);
  vector<int> has = vector<int>(26);

  void add(const string &s, const vector<int> &lst, int i) {
    if (i == sz(s))
      return;
    int c = s[i] - 'a';
    if (child[c] == NULL)
      child[c] = new stNode();
    fori(k, 26) {
      if (lst[k] >= i) {
        has[k]++;
      }
    }
    child[c]->add(s, lst, i + 1);
  }

  int check(const string &s, int i) {
    int c = s[i] - 'a';
    if (i == sz(s) - 1) {
      return has[c];
    }
    if (child[c] == NULL)
      return false;
    return child[c]->check(s, i + 1);
  }
};

int solve() {
  int n;
  cin >> n;
  vector<string> v(n);
  map<string, int> m;
  fori(i, n) {
    cin >> v[i];
    reverse(all(v[i]));
    m[v[i]]++;
  }

  stNode *root1 = new stNode();
  LL ans = 0;
  fori(i, n) {
    string &s = v[i];
    ans += root1->check(s, 0);

    debug(s, ans);

    vector<int> lst(26, -1);
    fori(j, sz(s)) lst[s[j] - 'a'] = j;

    root1->add(s, lst, 0);
  }

  stNode *root2 = new stNode();
  ford(i, n - 1, 0) {
    string &s = v[i];
    ans += root2->check(s, 0);

    vector<int> lst(26, -1);
    fori(j, sz(s)) lst[s[j] - 'a'] = j;

    root2->add(s, lst, 0);
  }

  for (auto &it : m) {
    ans -= 1LL * it.se * (it.se - 1) / 2;
  }
  cout << ans << en;
  return 0;
}

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  int ntest = 1;
  // cin >> ntest;
  fori(test, ntest) {
    // cout << "Case #" << test+1 << ": ";
    solve();
  }

  return 0;
}