Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

#define TEXT_SIZE 1000001
#define PAT_SIZE 10001
#define ASCII 123

void countup(int *accumulate, int pCmp, int value) { accumulate[pCmp] = value; }

void undefined(int *, int, int) {}

int setCompress(char *text, int **compress, int *accumulate, int len,
                void (*func)(int *, int, int)) {
  int result = 0;
  int pCmp = -1;
  char ch = '*';
  for (int i = 0; i < len; i++) {
    if (text[i] != ch) {
      compress[0][++pCmp] = int(text[i]);
      ch = text[i];
      result++;
    }
    compress[1][pCmp]++;
    func(accumulate, pCmp, i + 1);
  }
  return result;
}

int main() {
  char text[TEXT_SIZE];
  char pat[PAT_SIZE];
  int *compressT[2];
  int *compressP[2];
  int *accumulate;
  int skip[ASCII];
  int i, j, orgTextLen, orgPatLen, pLast, pText, pPat, textLen, patLen;
  cin >> text;
  cin >> pat;

  orgTextLen = strlen(text);
  orgPatLen = strlen(pat);

  for (i = 0; i < 2; i++) {
    compressT[i] = (int *)calloc(orgTextLen, sizeof(int));
    compressP[i] = (int *)calloc(orgPatLen, sizeof(int));
  }
  accumulate = (int *)calloc(orgTextLen, sizeof(int));

  textLen = setCompress(text, compressT, accumulate, orgTextLen, countup);
  patLen = setCompress(pat, compressP, NULL, orgPatLen, undefined);

  for (i = 0; i < ASCII; i++)
    skip[i] = patLen;
  for (i = 0; i < patLen - 1; i++)
    skip[int(compressP[0][i])] = patLen - i - 1;

  pLast = patLen - 1;
  while (pLast < textLen) {
    pText = pLast;
    pPat = patLen - 1;
    while (compressT[0][pText] == compressP[0][pPat]) {
      if (pPat == 0) {
        for (i = 0; i < patLen; i++) {
          if (i == 0 || i + 1 == patLen) {
            if (compressP[1][i] > compressT[1][pText + i])
              break;
          } else if (compressP[1][i] != compressT[1][pText + i])
            break;
        }
        if (i == patLen) {
          if (patLen == 1) {
            for (j = 0; j <= compressT[1][pText] - compressP[1][0]; j++) {
              cout << accumulate[pText] - compressT[1][pText] + j << endl;
            }
          } else {
            cout << accumulate[pText] - compressP[1][0] << endl;
          }
        }
        break;
      }
      pText--;
      pPat--;
    }
    pLast = pLast + skip[int(compressT[0][pLast])];
  }

  for (i = 0; i < 2; i++) {
    free(compressT[i]);
    free(compressP[i]);
  }
  free(accumulate);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

#define ASCII 123

char text[1000001];
char pat[10001];
int skip[ASCII];

int main() {
  int i, textLen, patLen, pLast, pText, pPat;
  cin >> text;
  cin >> pat;

  textLen = strlen(text);
  patLen = strlen(pat);

  for (i = 0; i < ASCII; i++)
    skip[i] = patLen;

  for (i = 0; i < patLen - 1; i++)
    skip[int(pat[i])] = patLen - i - 1;

  pLast = patLen - 1;
  while (pLast < textLen) {
    pText = pLast;
    pPat = patLen - 1;
    while (text[pText] == pat[pPat]) {
      if (pPat == 0) {
        cout << pText << endl;
        break;
      }
      pText--;
      pPat--;
    }
    pLast = pLast + skip[int(text[pLast])];
  }

  return 0;
}