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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

#define MAX 1000002
typedef unsigned char byte;
byte mask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

int Lv[MAX];
int n, k;

bool cmp_Lv(const int i, const int j) { return Lv[i] < Lv[j]; }

bool cmp(const int i, const int j) {
  if (Lv[i] != Lv[j]) {
    return Lv[i] < Lv[j];
  }
  int L_i = (i + k >= n ? -1 : Lv[i + k]);
  int L_j = (j + k >= n ? -1 : Lv[j + k]);

  return L_i < L_j;
}

int main() {
  int A[MAX];
  char c[MAX];
  scanf("%s", c);
  std::string T = std::string(c);
  n = T.size();
  byte *t;
  t = new byte[(n >> 3) + 1];
  bool act;
  int l, r;

  int i;
  for (i = 0; i < n; i++) {
    A[i] = i;
  }
  for (i = 0; i < n; i++) {
    Lv[i] = T[i];
  }
  std::sort(A, A + n, cmp_Lv);

  for (i = 1; i < n; i++) {
    t[i >> 3] = (Lv[A[i]] != Lv[A[i - 1]] ? t[i >> 3] | mask[i & 7]
                                          : t[i >> 3] & ~mask[i & 7]);
  }
  t[n >> 3] = t[n >> 3] | mask[n & 7];

  Lv[A[0]] = 0;
  for (i = 1; i < n; i++) {
    Lv[A[i]] = Lv[A[i - 1]] + !!(mask[i & 7] & t[i >> 3]);
  }

  k = 1;
  while (1) {
    if (k > n)
      break;

    act = true;
    for (i = 1; i <= n; i++) {
      if (act) {
        if (!!(mask[i & 7] & t[i >> 3])) {
          continue;
        } else {
          l = i - 1;
          act = false;
        }
      } else {
        if (!!(mask[i & 7] & t[i >> 3])) {
          r = i;
          std::sort(A + l, A + r, cmp);
          act = true;
        } else {
          continue;
        }
      }
    }

    for (i = 1; i < n; i++) {
      t[i >> 3] = (cmp(A[i - 1], A[i]) ? t[i >> 3] | mask[i & 7]
                                       : t[i >> 3] & ~mask[i & 7]);
    }
    t[n >> 3] = t[n >> 3] | mask[n & 7];

    Lv[A[0]] = 0;
    for (i = 1; i < n; i++) {
      Lv[A[i]] = Lv[A[i - 1]] + !!(mask[i & 7] & t[i >> 3]);
    }

    if (Lv[A[n - 1]] == n - 1)
      break;
    k <<= 1;
  };

  char p[1000];
  int q, left, right, mid, crit;
  bool exist;
  scanf("%d", &q);

  A[n] = n;

  while (q) {
    scanf("%s", p);
    std::string P = std::string(p);
    left = 0, right = n, exist = false;
    while (left < right) {

      if (n < P.size()) {
        exist = false;
        break;
      }

      mid = (left + right) / 2;
      crit = T.compare(A[mid], P.size(), P);

      if (crit > 0) {
        right = mid;
      } else if (crit < 0) {
        left = mid + 1;
      } else {
        exist = true;
        break;
      }
    };
    if (T.compare(A[right], P.size(), P) == 0)
      exist = true;
    if (exist) {
      printf("1\\n");
    } else {
      printf("0\\n");
    }
    q--;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

#define MAX 10002
typedef unsigned char byte;
byte mask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

int Lv[MAX];
int n, k;

bool cmp_Lv(const int i, const int j) { return Lv[i] < Lv[j]; }

bool cmp(const int i, const int j) {
  if (Lv[i] != Lv[j]) {
    return Lv[i] < Lv[j];
  }
  int L_i = (i + k >= n ? -1 : Lv[i + k]);
  int L_j = (j + k >= n ? -1 : Lv[j + k]);

  return L_i < L_j;
}

int main() {
  int A[MAX];
  char c[MAX];
  scanf("%s", c);
  std::string T = std::string(c);
  n = T.size();
  byte *t;
  t = new byte[(n >> 3) + 1];
  bool act;
  int l, r;

  int i;
  for (i = 0; i < n; i++) {
    A[i] = i;
  }
  for (i = 0; i < n; i++) {
    Lv[i] = T[i];
  }
  std::sort(A, A + n, cmp_Lv);

  for (i = 1; i < n; i++) {
    t[i >> 3] = (Lv[A[i]] != Lv[A[i - 1]] ? t[i >> 3] | mask[i & 7]
                                          : t[i >> 3] & ~mask[i & 7]);
  }
  t[n >> 3] = t[n >> 3] | mask[n & 7];

  Lv[A[0]] = 0;
  for (i = 1; i < n; i++) {
    Lv[A[i]] = Lv[A[i - 1]] + !!(mask[i & 7] & t[i >> 3]);
  }

  k = 1;
  while (1) {
    if (k > n)
      break;

    // std::sort(A, A + n, cmp);

    act = true;
    for (i = 1; i <= n; i++) {
      if (act) {
        if (!!(mask[i & 7] & t[i >> 3])) {
          continue;
        } else {
          l = i - 1;
          act = false;
        }
      } else {
        if (!!(mask[i & 7] & t[i >> 3])) {
          r = i;
          std::sort(A + l, A + r, cmp);
          act = true;
        } else {
          continue;
        }
      }
    }

    for (i = 1; i < n; i++) {
      t[i >> 3] = (cmp(A[i - 1], A[i]) ? t[i >> 3] | mask[i & 7]
                                       : t[i >> 3] & ~mask[i & 7]);
    }
    t[n >> 3] = t[n >> 3] | mask[n & 7];

    Lv[A[0]] = 0;
    for (i = 1; i < n; i++) {
      Lv[A[i]] = Lv[A[i - 1]] + !!(mask[i & 7] & t[i >> 3]);
    }

    if (Lv[A[n - 1]] == n - 1)
      break;
    k <<= 1;
  };

  char p[1000];
  int q, left, right, mid, crit;
  bool exist;
  scanf("%d", &q);

  A[n] = n;

  while (q) {
    scanf("%s", p);
    std::string P = std::string(p);
    left = 0, right = n, exist = false;
    while (left < right) {

      if (n < P.size()) {
        exist = false;
        break;
      }

      mid = (left + right) / 2;
      crit = T.compare(A[mid], P.size(), P);

      if (crit > 0) {
        right = mid;
      } else if (crit < 0) {
        left = mid + 1;
      } else {
        exist = true;
        break;
      }
    };
    if (T.compare(A[right], P.size(), P) == 0)
      exist = true;
    if (exist) {
      printf("1\\n");
    } else {
      printf("0\\n");
    }
    q--;
  }
  return 0;
}