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

#include <iostream>
#include <limits.h>
#include <list>
#include <stdio.h>
#include <string.h>
using namespace std;

#define T_LEN_MAX 1000000
#define P_LEN_MAX 10000

char T[T_LEN_MAX + 1] = "\\0";
char P[P_LEN_MAX + 1] = "\\0";
int T_len;
int P_len;

list<int> T_index;
int P_index[UCHAR_MAX + 1];

int Compare(int idx) {
  for (int i = 1; i < P_len; i++) {
    if (T[idx - i] != P[P_len - 1 - i])
      return T[idx - i];
  }
  return -1;
}

void MakeTIndex(int idx1, int idx2) {
  for (int i = 0; i + idx2 < T_len; i++) {
    if (T[i + idx1] == P[idx1] && T[i + idx2] == P[idx2]) {
      T_index.push_back(i + P_len - 1);
    }
  }
}

void MakeTIndex() {
  for (int i = 0; T[i] != '\\0'; i++) {
    if (T[i] == P[0]) {
      T_index.push_back(i);
    }
  }
}

void SearchAndOutput() {
  for (list<int>::iterator itr_t = T_index.begin(); *itr_t != -1;) {
    int ret = Compare(*itr_t);
    if (ret == -1) {
      cout << *itr_t - P_len + 1 << endl;
      itr_t++;
    } else {
      itr_t++;
      int idx = *itr_t + P_len - P_index[ret];
      while (*itr_t >= idx) {
        itr_t++;
      }
    }
  }
}

void SearchPPAndOutput() {
  int okflag = 0;
  int okidx = -2;
  for (list<int>::iterator itr_t = T_index.begin(); *itr_t != -1;) {
    int ret;
    if (okflag == 1 && *itr_t == okidx + 1) {
      ret = -1;
    } else {
      ret = Compare(*itr_t);
    }
    if (ret == -1) {
      okflag = 1;
      okidx = *itr_t;
      cout << *itr_t - P_len + 1 << endl;
      itr_t++;
    } else {
      okflag = 0;
      itr_t++;
      int idx = *itr_t + P_len - P_index[ret];
      while (*itr_t >= idx) {
        itr_t++;
      }
    }
  }
}

int main(void) {
  // input
  cin >> T >> P;
  T_len = strlen(T);
  P_len = strlen(P);

  // make P_index
  for (int i = 0; P[i] != '\\0'; i++) {
    P_index[P[i]] = i;
  }

  // make T_index
  int idx1, idx2;
  if (P_len == 1) {
    MakeTIndex();
  } else {
    idx1 = P_len - 1;
    idx2 = P_len - 2;
    while (idx2 > 0 && P[idx2] == P[idx1]) {
      idx2--;
    }
    MakeTIndex(idx2, idx1);
  }
  T_index.push_back(-1);

  // search and output
  if (P[idx1] == P[idx2]) {
    SearchPPAndOutput();
  } else {
    SearchAndOutput();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
#include <list>
#include <stdio.h>
#include <string.h>
using namespace std;

#define T_LEN_MAX 1000000
#define P_LEN_MAX 10000

char T[T_LEN_MAX + 1] = "\\0";
char P[P_LEN_MAX + 1] = "\\0";
int T_len;
int P_len;

list<int> T_index;
int P_index[UCHAR_MAX + 1];

int Compare(int idx) {
  for (int i = 1; i < P_len; i++) {
    if (T[idx - i] != P[P_len - 1 - i])
      return T[idx - i];
  }
  return -1;
}

void MakeTIndex(int idx1, int idx2) {
  for (int i = 0; i + idx2 < T_len; i++) {
    if (T[i + idx1] == P[idx1] && T[i + idx2] == P[idx2]) {
      T_index.push_back(i + P_len - 1);
    }
  }
}

void MakeTIndex() {
  for (int i = 0; T[i] != '\\0'; i++) {
    if (T[i] == P[0]) {
      T_index.push_back(i);
    }
  }
}

void SearchAndOutput() {
  for (list<int>::iterator itr_t = T_index.begin(); *itr_t != -1;) {
    int ret = Compare(*itr_t);
    if (ret == -1) {
      cout << *itr_t - P_len + 1 << endl;
      itr_t++;
    } else {
      itr_t++;
      int idx = *itr_t + P_len - P_index[ret];
      while (*itr_t >= idx) {
        itr_t++;
      }
    }
  }
}

void SearchPPAndOutput() {
  int flag = 0;
  int idx = -2;
  for (list<int>::iterator itr_t = T_index.begin(); *itr_t != -1;) {
    int ret;
    if (flag == 1 && *itr_t == idx + 1) {
      ret = -1;
    } else {
      ret = Compare(*itr_t);
    }
    if (ret == -1) {
      cout << *itr_t - P_len + 1 << endl;
      itr_t++;
      flag = 1;
      idx = *itr_t;
    } else {
      itr_t++;
      int idx = *itr_t + P_len - P_index[ret];
      while (*itr_t >= idx) {
        itr_t++;
      }
      flag = 0;
    }
  }
}

int main(void) {
  // input
  cin >> T >> P;
  T_len = strlen(T);
  P_len = strlen(P);

  // make P_index
  for (int i = 0; P[i] != '\\0'; i++) {
    P_index[P[i]] = i;
  }

  // make T_index
  int idx1, idx2;
  if (P_len == 1) {
    MakeTIndex();
  } else {
    idx1 = P_len - 1;
    idx2 = P_len - 2;
    while (idx2 > 0 && P[idx2] == P[idx1]) {
      idx2--;
    }
    MakeTIndex(idx2, idx1);
  }
  T_index.push_back(-1);

  // search and output
  if (P[idx1] == P[idx2]) {
    SearchPPAndOutput();
  } else {
    SearchAndOutput();
  }

  return 0;
}