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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string a, b;
vector<int> zzz[26], zz[26][26], z[26][26][26], s;

void PRE() {
  string t;
  cin >> t;
  for (int i = 0; i < t.size(); i++)
    s.push_back(t[i] - 'a');
  for (int i = 0; i < t.size(); i++) {
    zzz[s[i]].push_back(i);
    if (i)
      zz[s[i - 1]][s[i]].push_back(i - 1);
    if (i > 1)
      z[s[i - 2]][s[i - 1]][s[i]].push_back(i - 2);
  }
} //???????????´?????????

int Xsolve() {
  int INF = 114514;
  vector<int> x;
  for (int i = 0; i < a.size(); i++)
    x.push_back(a[i] - 'a');
  if (x.size() == 1)
    return zzz[x[0]].size() ? zzz[x[0]][0] : INF;
  if (x.size() == 2)
    return zz[x[0]][x[1]].size() ? zz[x[0]][x[1]][0] + 1 : INF;
  if (x.size() == 3)
    return z[x[0]][x[1]][x[2]].size() ? z[x[0]][x[1]][x[2]][0] + 2 : INF;
  for (int i = 0; i < z[x[0]][x[1]][x[2]].size(); i++) {
    int k = z[x[0]][x[1]][x[2]][i], w = x.size() - 1;
    if (k + w < s.size()) {
      bool ok = true;
      for (int j = k + 3; j <= k + w; j++)
        if (s[j] != x[j - k])
          ok = false;
      if (ok)
        return k + w;
    }
  }
  return INF;
} //????????????????????????
int Ysolve() {
  int INF = -1;
  vector<int> x;
  for (int i = 0; i < b.size(); i++)
    x.push_back(b[i] - 'a');
  if (x.size() == 1)
    return zzz[x[0]].size() ? zzz[x[0]][zzz[x[0]].size() - 1] : INF;
  if (x.size() == 2)
    return zz[x[0]][x[1]].size() ? zz[x[0]][x[1]][zz[x[0]][x[1]].size() - 1]
                                 : INF;
  if (x.size() == 3)
    return z[x[0]][x[1]][x[2]].size()
               ? z[x[0]][x[1]][x[2]][z[x[0]][x[1]][x[2]].size() - 1]
               : INF;
  for (int i = z[x[0]][x[1]][x[2]].size() - 1; i >= 0; i--) {
    int k = z[x[0]][x[1]][x[2]][i], w = x.size() - 1;
    if (k + w < s.size()) {
      bool ok = true;
      for (int j = k + 3; j <= k + w; j++)
        if (s[j] != x[j - k])
          ok = false;
      if (ok)
        return k;
    }
  }
  return INF;
} //????????????????????????

int main() {
  PRE();
  int m;
  cin >> m;
  while (m--) {
    cin >> a >> b;
    int l = Xsolve(), r = Ysolve();
    if (r != -1 && l != 114514 && r >= l - a.size() + 1 &&
        l + 1 <= r + b.size())
      cout << r - l + b.size() + a.size() - 1 << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> zzz[26], zz[26][26], z[26][26][26], s;

void PRE() {
  // string t;cin>>t;
  // for(int i=0;i<t.size();i++)s.push_back(t[i]-'a');
  int rrr = 0;
  char eee;
  while (cin >> eee) {
    s[rrr] = eee - 'a';
    rrr++;
  }
  for (int i = 0; i < rrr; i++) {
    zzz[s[i]].push_back(i);
    if (i)
      zz[s[i - 1]][s[i]].push_back(i - 1);
    if (i > 1)
      z[s[i - 2]][s[i - 1]][s[i]].push_back(i - 2);
  }
} //???????????´?????????

int Xsolve(string y) {
  vector<int> x;
  for (int i = 0; i < y.size(); i++)
    x.push_back(y[i] - 'a');
  if (x.size() == 1)
    return zzz[x[0]][0];
  if (x.size() == 2)
    return zz[x[0]][x[1]][0] + 1;
  if (x.size() == 3)
    return z[x[0]][x[1]][x[2]][0] + 2;
  for (int i = 0; i < z[x[0]][x[1]][x[2]].size(); i++) {
    int k = z[x[0]][x[1]][x[2]][i], w = x.size() - 1;
    if (k + w < s.size()) {
      bool ok = true;
      for (int j = k + 3; j <= k + w; j++)
        if (s[j] != x[j - k])
          ok = false;
      if (ok)
        return k + w;
    }
  }
  return 1145141919;
} //????????????????????????
int Ysolve(string y) {
  vector<int> x;
  for (int i = 0; i < y.size(); i++)
    x.push_back(y[i] - 'a');
  if (x.size() == 1)
    return zzz[x[0]][zzz[x[0]].size() - 1];
  if (x.size() == 2)
    return zz[x[0]][x[1]][zz[x[0]][x[1]].size() - 1];
  if (x.size() == 3)
    return z[x[0]][x[1]][x[2]][z[x[0]][x[1]][x[2]].size() - 1];
  for (int i = z[x[0]][x[1]][x[2]].size() - 1; i >= 0; i--) {
    int k = z[x[0]][x[1]][x[2]][i], w = x.size() - 1;
    if (k + w < s.size()) {
      bool ok = true;
      for (int j = k + 3; j <= k + w; j++)
        if (s[j] != x[j - k])
          ok = false;
      if (ok)
        return k;
    }
  }
  return -1;
} //????????????????????????

int main() {
  PRE();
  int m;
  cin >> m;
  while (m--) {
    string x, y;
    cin >> x >> y;
    int l = Xsolve(x), r = Ysolve(y);
    if (r >= l - x.size() + 1 && l <= r + y.size() - 1)
      cout << r - l + y.size() + x.size() - 1 << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}