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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
const int N = 200005;
const int M = 1000005;
void getint(int &num) {
  char c;
  bool flag = 0;
  num = 0;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = 1;
  while (c >= '0' && c <= '9') {
    num = num * 10 + c - 48;
    c = getchar();
  }
  if (flag)
    num = -num;
}
int n, first[30], len[N], id[N], tot[30];
int ch[M][27], sz[M], letter[M][27], cnt = 1;
ll ans;
string str[N];
map<string, int> vis;
inline void insert(string str) {
  int m = str.length(), x = 1;
  for (int i = 0; i < 26; i++)
    first[i] = -1;

  for (int i = 0; i < m; i++)
    if (first[str[i] - 'a'] == -1) {
      first[str[i] - 'a'] = i;
      tot[str[i] - 'a']++;
    }

  if (m == 1) {
    ans += tot[str[0] - 'a'] - 1;
    return;
  }

  for (int i = m - 1; i >= 0; i--) {
    int y = str[i] - 'a';
    if (!ch[x][y])
      ch[x][y] = ++cnt;
    x = ch[x][y];
    sz[x]++;

    if (i == 1)
      ans += letter[x][str[0] - 'a'];

    for (int j = 0; j < 26; j++)
      if ((~first[j]) && first[j] < i)
        letter[x][j]++;
  }
}
bool cmp(int i, int j) { return len[i] > len[j]; }
int main() {
  getint(n);
  for (int i = 1; i <= n; i++) {
    cin >> str[i];
    len[i] = str[i].length();
    id[i] = i;
  }
  sort(id + 1, id + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    insert(str[id[i]]);
    if (!vis.count(str[id[i]]))
      vis[str[id[i]]] = 1;
    else
      ans += vis[str[id[i]]];
  }

  cout << ans << endl;
}
/*

*/


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
const int N = 200005;
const int M = 1000005;
void getint(int &num) {
  char c;
  bool flag = 0;
  num = 0;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = 1;
  while (c >= '0' && c <= '9') {
    num = num * 10 + c - 48;
    c = getchar();
  }
  if (flag)
    num = -num;
}
int n, first[30], len[N], id[N], tot[30];
int ch[N][27], sz[M], letter[M][27], cnt = 1;
ll ans;
string str[N];
map<string, int> vis;
inline void insert(string str) {
  int m = str.length(), x = 1;
  for (int i = 0; i < 26; i++)
    first[i] = -1;

  for (int i = 0; i < m; i++)
    if (first[str[i] - 'a'] == -1) {
      first[str[i] - 'a'] = i;
      tot[str[i] - 'a']++;
    }

  if (m == 1) {
    ans += tot[str[0] - 'a'] - 1;
    return;
  }

  for (int i = m - 1; i >= 0; i--) {
    int y = str[i] - 'a';
    if (!ch[x][y])
      ch[x][y] = ++cnt;
    x = ch[x][y];
    sz[x]++;

    if (i == 1)
      ans += letter[x][str[0] - 'a'];

    for (int j = 0; j < 26; j++)
      if ((~first[j]) && first[j] < i)
        letter[x][j]++;
  }
}
bool cmp(int i, int j) { return len[i] > len[j]; }
int main() {
  getint(n);
  for (int i = 1; i <= n; i++) {
    cin >> str[i];
    len[i] = str[i].length();
    id[i] = i;
  }
  sort(id + 1, id + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    insert(str[id[i]]);
    if (!vis.count(str[id[i]]))
      vis[str[id[i]]] = 1;
    else
      ans += vis[str[id[i]]];
  }

  cout << ans << endl;
}
/*

*/