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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1000001

struct Node {
  int parent_id, children[128], suffix_link;
  bool finish_FLG;
};

struct Info {
  Info(int arg_node_id, int arg_ch) {
    node_id = arg_node_id;
    ch = arg_ch;
  }

  int node_id, ch;
};

int T_length, P_length;
int index = 1;
char T[NUM], P[10001];
Node nodes[10005];

int main() {

  scanf("%s", T);
  for (T_length = 0; T[T_length] != '\\0'; T_length++)
    ;

  scanf("%s", P);
  for (P_length = 0; P[P_length] != '\\0'; P_length++)
    ;

  int root = 0;
  nodes[root].suffix_link = -1;

  for (int i = 0; i <= P_length + 1; i++) {
    nodes[i].parent_id = -1;
    nodes[i].finish_FLG = false;
    for (int k = 0; k < 128; k++) {
      nodes[i].children[k] = -1;
    }
  }

  int tmp_ch, tmp_loc, parent_id, tmp_index;

  tmp_index = 0;
  tmp_ch = P[tmp_index];

  tmp_loc = root;

  while (true) {

    parent_id = tmp_loc;

    nodes[tmp_loc].children[tmp_ch] = index++;

    tmp_loc = nodes[tmp_loc].children[tmp_ch];
    nodes[tmp_loc].parent_id = parent_id;

    tmp_index++;

    if (P[tmp_index] == '\\0') {
      nodes[tmp_loc].finish_FLG = true;
      break;
    }
    tmp_ch = P[tmp_index];
  }

  int node_id;
  queue<Info> MAKE_SL;
  for (int i = 0; i < 128; i++) {
    if (nodes[root].children[i] != -1) {
      node_id = nodes[root].children[i];
      nodes[node_id].suffix_link = root;
      for (int k = 0; k < 128; k++) {
        if (nodes[node_id].children[k] != -1) {
          MAKE_SL.push(Info(nodes[node_id].children[k], k));
        }
      }
    }
  }

  while (!MAKE_SL.empty()) {
    node_id = MAKE_SL.front().node_id;
    tmp_ch = MAKE_SL.front().ch;
    MAKE_SL.pop();

    for (int i = 0; i < 128; i++) {
      if (nodes[node_id].children[i] != -1) {
        MAKE_SL.push(Info(nodes[node_id].children[i], i));
      }
    }

    for (tmp_loc = nodes[nodes[node_id].parent_id].suffix_link; tmp_loc != root;
         tmp_loc = nodes[tmp_loc].suffix_link) {
      if (nodes[tmp_loc].children[tmp_ch] != -1) {
        break;
      }
    }

    if (tmp_loc == root) {
      if (nodes[root].children[tmp_ch] != -1) {
        nodes[node_id].suffix_link = nodes[root].children[tmp_ch];
      } else {
        nodes[node_id].suffix_link = root;
      }
    } else {
      nodes[node_id].suffix_link = nodes[tmp_loc].children[tmp_ch];
    }
  }

  tmp_loc = root;
  for (int i = 0; i < T_length; i++) {
    tmp_ch = T[i];

    if (nodes[tmp_loc].children[tmp_ch] != -1) {
      tmp_loc = nodes[tmp_loc].children[tmp_ch];

      if (nodes[tmp_loc].finish_FLG) {
        printf("%d\\n", i - P_length + 1);
        tmp_loc = nodes[tmp_loc].suffix_link;
      }
    } else {

      if (tmp_loc == root)
        continue;

      for (tmp_loc = nodes[tmp_loc].suffix_link; tmp_loc != root;
           tmp_loc = nodes[tmp_loc].suffix_link) {
        if (nodes[tmp_loc].children[tmp_ch] != -1) {
          break;
        }
      }
      if (nodes[tmp_loc].children[tmp_ch] != -1) {
        tmp_loc = nodes[tmp_loc].children[tmp_ch];
      } else {
        // Do nothing
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1000001

struct Node {
  int parent_id, children[128], suffix_link;
  bool finish_FLG;
};

struct Info {
  Info(int arg_node_id, int arg_ch) {
    node_id = arg_node_id;
    ch = arg_ch;
  }

  int node_id, ch;
};

int T_length, P_length;
int index = 1;
char T[NUM], P[10001];
Node nodes[10005];

int main() {

  scanf("%s", T);
  for (T_length = 0; T[T_length] != '\\0'; T_length++)
    ;

  scanf("%s", P);
  for (P_length = 0; P[P_length] != '\\0'; P_length++)
    ;

  int root = 0;
  nodes[root].suffix_link = -1;

  for (int i = 0; i <= P_length + 1; i++) {
    nodes[i].parent_id = -1;
    nodes[i].finish_FLG = false;
    for (int k = 0; k < 128; k++) {
      nodes[i].children[k] = -1;
    }
  }

  int tmp_ch, tmp_loc, parent_id, tmp_index;

  tmp_index = 0;
  tmp_ch = P[tmp_index];

  tmp_loc = root;
  tmp_index = 0;

  while (true) {

    parent_id = tmp_loc;

    nodes[tmp_loc].children[tmp_ch] = index++;

    tmp_loc = nodes[tmp_loc].children[tmp_ch];
    nodes[tmp_loc].parent_id = parent_id;

    tmp_index++;

    if (P[tmp_index] == '\\0') {
      nodes[tmp_loc].finish_FLG = true;
      break;
    }
    tmp_ch = P[tmp_index];
  }

  int node_id;
  queue<Info> MAKE_SL;
  for (int i = 0; i < 128; i++) {
    if (nodes[root].children[i] != -1) {
      node_id = nodes[root].children[i];
      nodes[node_id].suffix_link = root;
      for (int k = 0; k < 128; k++) {
        if (nodes[node_id].children[k] != -1) {
          MAKE_SL.push(Info(nodes[node_id].children[k], k));
        }
      }
    }
  }

  while (!MAKE_SL.empty()) {
    node_id = MAKE_SL.front().node_id;
    tmp_ch = MAKE_SL.front().ch;
    MAKE_SL.pop();

    for (int i = 0; i < 128; i++) {
      if (nodes[node_id].children[i] != -1) {
        MAKE_SL.push(Info(nodes[node_id].children[i], i));
      }
    }

    for (tmp_loc = nodes[nodes[node_id].parent_id].suffix_link; tmp_loc != root;
         tmp_loc = nodes[tmp_loc].suffix_link) {
      if (nodes[tmp_loc].children[tmp_ch] != -1) {
        break;
      }
    }

    if (tmp_loc == root) {
      if (nodes[root].children[tmp_ch] != -1) {
        nodes[node_id].suffix_link = nodes[root].children[tmp_ch];
      } else {
        nodes[node_id].suffix_link = root;
      }
    } else {
      nodes[node_id].suffix_link = nodes[tmp_loc].children[tmp_ch];
    }
  }

  tmp_loc = root;
  for (int i = 0; i < T_length; i++) {
    tmp_ch = T[i];

    if (nodes[tmp_loc].children[tmp_ch] != -1) {
      tmp_loc = nodes[tmp_loc].children[tmp_ch];

      if (nodes[tmp_loc].finish_FLG) {
        printf("%d\\n", i - P_length + 1);
        tmp_loc = nodes[tmp_loc].suffix_link;
      }
    } else {
      for (tmp_loc = nodes[tmp_loc].suffix_link; tmp_loc != root;
           tmp_loc = nodes[tmp_loc].suffix_link) {
        if (nodes[tmp_loc].children[tmp_ch] != -1) {
          break;
        }
      }
      if (nodes[tmp_loc].children[tmp_ch] != -1) {
        tmp_loc = nodes[tmp_loc].children[tmp_ch];
      } else {
        // Do nothing
      }
    }
  }

  return 0;
}