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

#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {

  int left_length, right_length, max, tmp, left_start, left_end, right_start,
      right_end, k;
  char left[4001], right[4001];

  while (scanf("%s", left) != EOF) {
    scanf("%s", right);

    for (left_length = 0; left[left_length] != '\\0'; left_length++)
      ;
    for (right_length = 0; right[right_length] != '\\0'; right_length++)
      ;

    max = 0;
    if (right_length > left_length) {

      for (right_end = 1; right_end <= left_length; right_end++) {
        tmp = 0;
        for (left_start = left_length - right_end, k = 0; k < right_end; k++) {
          if (left[left_start + k] == right[k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      for (right_end = left_length; right_end < right_length; right_end++) {
        tmp = 0;
        for (right_start = right_end - left_length, k = 0;
             right_start + k < right_end; k++) {
          if (left[k] == right[right_start + k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      for (left_end = left_length - 1; left_end >= 1; left_end--) {
        tmp = 0;
        for (right_start = right_length - left_end, k = 0; k < left_end; k++) {
          if (left[k] == right[right_start + k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      printf("%d\\n", max);

    } else {

      for (left_end = 1; left_end <= right_length; left_end++) {
        tmp = 0;
        for (right_start = right_length - left_end, k = 0; k < left_end; k++) {
          if (right[right_start + k] == left[k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      for (left_end = right_length; left_end < left_length; left_end++) {
        tmp = 0;
        for (left_start = left_end - right_length, k = 0;
             left_start + k < left_end; k++) {
          if (right[k] == left[left_start + k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      for (right_end = right_length - 1; right_end >= 1; right_end--) {
        tmp = 0;
        for (left_start = left_length - right_end, k = 0;
             left_start + k < left_length; k++) {
          if (right[k] == left[left_start + k]) {
            tmp++;
            max = (max >= tmp) ? max : tmp;
          } else {
            tmp = 0;
          }
        }
      }

      printf("%d\\n", max);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int T_char_count[128] = {0};

class LocRecord {

public:
  LocRecord() {
    locationTable = new int *[128];
    for (int i = 65; i <= 90; i++) {
      if (T_char_count[i] > 0) {
        locationTable[i] = new int[T_char_count[i] + 1];
      }
    }

    int limit;
    for (int i = 65; i <= 90; i++) {
      limit = T_char_count[i];
      index[i] = 0;
      if (T_char_count[i] > 0) {
        for (int k = 0; k <= limit; k++) {
          locationTable[i][k] = -1;
        }
      }
    }
  }

  void init_index() {
    for (int i = 65; i <= 90; i++) {
      index[i] = 0;
    }
  }

  void regist_loc(int ch, int location) {
    locationTable[ch][index[ch]] = location;
    index[ch]++;
  }

  int next_loc(int ch) {
    if (T_char_count[ch] == 0)
      return -1;

    int ret = locationTable[ch][index[ch]];
    index[ch]++;
    return ret;
  }

private:
  int **locationTable;
  int index[128];
};

int main() {

  int count, left_length, right_length, max, tmp_len, start_loc;
  char left[4001], right[4001];

  while (scanf("%s\\n%s\\n", left, right) != EOF) {

    for (int i = 65; i <= 90; i++)
      T_char_count[i] = 0;
    max = 0;

    for (left_length = 0; left[left_length] != '\\0'; left_length++)
      ;
    for (right_length = 0; right[right_length] != '\\0'; right_length++)
      ;

    if (left_length > right_length) {

      for (int i = 0; i < left_length; i++)
        T_char_count[left[i]]++;

      LocRecord locRecord;

      for (int i = 0; i < left_length; i++)
        locRecord.regist_loc(left[i], i);

      for (int i = 0; i < right_length; i++) {
        locRecord.init_index();
        while ((start_loc = locRecord.next_loc(right[i])) != -1) {
          tmp_len = 0;
          for (int k = 0; start_loc + k < left_length; k++) {
            if (right[i + k] == left[start_loc + k])
              tmp_len++;
            else {
              max = (max >= tmp_len) ? max : tmp_len;
              break;
            }
          }
        }
      }

      printf("%d\\n", max);

    } else {

      for (int i = 0; i < right_length; i++)
        T_char_count[right[i]]++;

      LocRecord locRecord;

      for (int i = 0; i < right_length; i++)
        locRecord.regist_loc(right[i], i);

      for (int i = 0; i < left_length; i++) {
        locRecord.init_index();
        while ((start_loc = locRecord.next_loc(left[i])) != -1) {
          tmp_len = 0;
          for (int k = 0; start_loc + k < left_length; k++) {
            if (left[i + k] == right[start_loc + k])
              tmp_len++;
            else {
              max = (max >= tmp_len) ? max : tmp_len;
              break;
            }
          }
        }
      }

      printf("%d\\n", max);
    }
  }

  return 0;
}