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

#include <bits/stdc++.h>
using namespace std;
long long nxt[27][1000005], cnt1[1000005][27], created[1000005], sz,
    cnt[1000005][27], ans, n;
long long ans1;
string s1[200005];
pair<int, string> s[200005];
void insert(string &s) {
  int v = 0;
  for (int i = 0; i < s.size(); i++) {
    int c = s[i] - 'a';
    if (!created[nxt[c][v]]) {
      nxt[c][v] = ++sz;
      created[sz] = 1;
    }
    //     cout<<v<<" "<<s<<endl;
    for (int j = 0; j < 26; j++) {
      cnt1[v][j] += cnt[i][j];
    }
    v = nxt[c][v];
    // cout<<v<<endl;
    //  cout<<v<<endl;

    // cout<<cnt1[1][0]<<endl;
  }
}
int search(string &tmp) {
  // cout<<tmp<<endl;
  long long ans = 0;
  int v = 0;
  for (int i = 0; i < tmp.size() - 1; i++) {
    int ty = tmp[i] - 'a';
    if (created[nxt[ty][v]]) {
      v = nxt[ty][v];
    } else {
      ans = -1;
      return 0;
    }
  }

  // cout<<v<<" "<<cnt1[v][tmp[tmp.size()-1]-'a']<<endl;
  // cout<<cnt1[2][2]<<" "<<v<<"  "<<tmp[tmp.size()-1]-'a'<<endl;
  return cnt1[v][tmp[tmp.size() - 1] - 'a'];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i].second;
    s[i].first = s[i].second.size();
  }
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) {
    s1[i] = s[i].second;
    reverse(s1[i].begin(), s1[i].end());
  }
  reverse(s1 + 1, s1 + n + 1);
  for (int i = s1[1].size() - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (s1[1][i] - 'a' == j && !cnt[i + 1][j])
        cnt[i][j] = 1;
      else
        cnt[i][j] = cnt[i + 1][j];
    }
  }
  insert(s1[1]);
  for (int i = 0; i < s1[1].size(); i++) {
    for (int j = 0; j < 26; j++) {
      cnt[i][j] = 0;
    }
  }
  for (int i = 2; i <= n; i++) {
    ans1 += search(s1[i]);
    // cout<<i<<" "<<ans1<<endl;
    for (int j = s1[i].size() - 1; j >= 0; j--) {
      for (int j1 = 0; j1 < 26; j1++) {
        if (s1[i][j] - 'a' == j1 && !cnt[j + 1][j1])
          cnt[j][j1] = 1;
        else
          cnt[j][j1] = cnt[j + 1][j1];
      }
    }
    insert(s1[i]);
    for (int j = 0; j < s1[i].size(); j++) {
      for (int j1 = 0; j1 < 26; j1++) {
        cnt[j][j1] = 0;
      }
    }
  }
  cout << ans1 << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long nxt[30][4 * 100005], cnt1[4 * 100005][30], created[4 * 100005], sz,
    cnt[100005][30], ans, n;
long long ans1;
string s1[100005];
pair<int, string> s[100005];
void insert(string &s) {
  int v = 0;
  for (int i = 0; i < s.size(); i++) {
    int c = s[i] - 'a';
    if (!created[nxt[c][v]]) {
      nxt[c][v] = ++sz;
      created[sz] = 1;
    }
    //     cout<<v<<" "<<s<<endl;
    for (int j = 0; j < 26; j++) {
      cnt1[v][j] += cnt[i][j];
    }
    v = nxt[c][v];
    // cout<<v<<endl;
    //  cout<<v<<endl;

    // cout<<cnt1[1][0]<<endl;
  }
}
int search(string &tmp) {
  // cout<<tmp<<endl;
  long long ans = 0;
  int v = 0;
  for (int i = 0; i < tmp.size() - 1; i++) {
    int ty = tmp[i] - 'a';
    if (created[nxt[ty][v]]) {
      v = nxt[ty][v];
    } else {
      ans = -1;
      return 0;
    }
  }

  // cout<<v<<" "<<cnt1[v][tmp[tmp.size()-1]-'a']<<endl;
  // cout<<cnt1[2][2]<<" "<<v<<"  "<<tmp[tmp.size()-1]-'a'<<endl;
  return cnt1[v][tmp[tmp.size() - 1] - 'a'];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i].second;
    s[i].first = s[i].second.size();
  }
  sort(s + 1, s + n + 1);
  for (int i = 1; i <= n; i++) {
    s1[i] = s[i].second;
    reverse(s1[i].begin(), s1[i].end());
  }
  reverse(s1 + 1, s1 + n + 1);
  for (int i = s1[1].size() - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (s1[1][i] - 'a' == j)
        cnt[i][j] = cnt[i + 1][j] + 1;
      else
        cnt[i][j] = cnt[i + 1][j];
    }
  }
  insert(s1[1]);
  for (int i = 0; i < s1[1].size(); i++) {
    for (int j = 0; j < 26; j++) {
      cnt[i][j] = 0;
    }
  }
  for (int i = 2; i <= n; i++) {
    ans1 += search(s1[i]);
    // cout<<i<<" "<<ans1<<endl;
    for (int j = s1[i].size() - 1; j >= 0; j--) {
      for (int j1 = 0; j1 < 26; j1++) {
        if (s1[i][j] - 'a' == j1)
          cnt[j][j1] = cnt[j + 1][j1] + 1;
        else
          cnt[j][j1] = cnt[j + 1][j1];
      }
    }
    insert(s1[i]);
    for (int j = 0; j < s1[i].size(); j++) {
      for (int j1 = 0; j1 < 26; j1++) {
        cnt[j][j1] = 0;
      }
    }
  }
  cout << ans1 << endl;
}