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

#include <bits/stdc++.h>
using namespace std;
int n, nxt[30][1000006], mp[30], C[1000006][30];
long long ans, sz, B[1000006][30];
bool created[1000006];
string s;
pair<int, string> A[200005];
void insert(string tmp) {
  int v = 0;
  vector<int> v1;
  for (int i = 0; i < tmp.size(); i++) {
    int c = tmp[i] - 'a';
    v1.push_back(v);
    if (!created[nxt[c][v]]) {
      nxt[c][v] = ++sz;
      created[sz] = 1;
    }
    v = nxt[c][v];
  }
  for (int i = v1.size() - 1; i >= 0; i--) {
    // cout<<v1[i]<<"*";
    for (int j = 0; j < 26; j++) {
      B[v1[i]][j] += C[i][j];
    }
  }
  // cout<<endl;
}
void search(string tmp) {
  int v = 0;
  for (int i = 0; i < tmp.size() - 1; i++) {
    int ty = tmp[i] - 'a';
    // cout<<v<<" ";
    if (created[nxt[ty][v]]) {
      v = nxt[ty][v];
    } else
      return;
  }
  // cout<<v<<"-"<<B[v][tmp[tmp.size()-1]-'a']<<endl;
  ans += B[v][tmp[tmp.size() - 1] - 'a'];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    A[i].first = -s.size();
    A[i].second = s;
  }
  sort(A + 1, A + 1 + n);
  s = A[1].second;
  // cout<<s<<endl;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (mp[s[i] - 'a'] == 0)
      mp[s[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      C[i][j] = mp[j];
    }
  }
  insert(A[1].second);
  for (int i = 2; i <= n; i++) {
    search(A[i].second);
    s = A[i].second;
    // cout<<s<<endl;
    for (int j = 0; j < 26; j++)
      mp[j] = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (mp[s[j] - 'a'] == 0)
        mp[s[j] - 'a']++;
      for (int w = 0; w < 26; w++) {
        C[j][w] = mp[w];
      }
    }
    insert(A[i].second);
  }
  cout << ans;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, nxt[30][1000006], mp[30], C[1000006][30];
long long ans, sz, B[1000006][30];
bool created[1000006];
string s;
pair<int, string> A[200005];
void insert(string tmp) {
  int v = 0;
  vector<int> v1;
  for (int i = 0; i < tmp.size(); i++) {
    int c = tmp[i] - 'a';
    v1.push_back(v);
    if (!created[nxt[c][v]]) {
      nxt[c][v] = ++sz;
      created[sz] = 1;
    }
    v = nxt[c][v];
  }
  for (int i = v1.size() - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      B[v1[i]][j] += C[i][j];
    }
  }
}
void search(string tmp) {
  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
      return;
  }
  ans += B[v][tmp[tmp.size() - 1] - 'a'];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    A[i].first = -s.size();
    A[i].second = s;
  }
  sort(A + 1, A + 1 + n);
  s = A[1].second;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (mp[s[i] - 'a'] == 0)
      mp[s[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      C[i][j] = mp[j];
    }
  }
  insert(A[1].second);
  for (int i = 2; i <= n; i++) {
    search(A[i].second);
    s = A[i].second;
    for (int j = 0; j < 26; j++)
      mp[j] = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (mp[s[i] - 'a'] == 0)
        mp[s[i] - 'a']++;
      for (int w = 0; w < 26; w++) {
        C[j][w] = mp[w];
      }
    }
    insert(A[i].second);
  }
  cout << ans;
}