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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

enum TYPE { NONE, FIRST, SECOND, LEFT, RIGHT };

struct Node {
  Node() {
    value = 0;
    parent = first_index = address = left_child = right_child = -1;
    route_index = 0;
  }
  void addRoute(char dist) {
    route[route_index++] = dist;
    route[route_index] = '\\0';
  }

  int value, left_child, right_child, address, route_index, first_index, parent;
  char route[150];
};

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

Node first[1000], second[1000], third[1000];
int first_index = 0, second_index = 0;
char buf[1010];

TYPE toWrite;

void recursive(int left, int right, int parent_address, TYPE left_or_right) {
  int calcDepth, nextRight, nextLeft, tmp, nextParentAddress = -1;
  if (parent_address != -1) {
    calcDepth = 0;
    for (int i = left, k = 0; i <= right; i++) {

      if (calcDepth == 0 && buf[i] == '[') {
        tmp = 0;
        for (k = i + 1; buf[k] != ']'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        if (toWrite == FIRST) {
          first[first_index].value = tmp;
          first[first_index].address = first_index;
          first[first_index].parent = parent_address;
          strcpy(first[first_index].route, first[parent_address].route);
          first[first_index].route_index = first[parent_address].route_index;
          if (left_or_right == LEFT) {
            first[first_index].addRoute('L');
            first[parent_address].left_child = first_index;
          } else {
            first[first_index].addRoute('R');
            first[parent_address].right_child = first_index;
          }
          nextParentAddress = first_index;
          first_index++;
        } else {
          second[second_index].value = tmp;
          second[second_index].address = second_index;
          second[second_index].parent = parent_address;
          strcpy(second[second_index].route, second[parent_address].route);
          second[second_index].route_index = second[parent_address].route_index;
          if (left_or_right == LEFT) {
            second[second_index].addRoute('L');
            second[parent_address].left_child = second_index;
          } else {
            second[second_index].addRoute('R');
            second[parent_address].right_child = second_index;
          }
          nextParentAddress = second_index;
          second_index++;
        }
        nextRight = i - 1;
        nextLeft = k + 1;
        break;
      } else if (calcDepth == 0 && buf[i] == '(' && buf[i + 1] >= '0' &&
                 buf[i + 1] <= '9') {
        tmp = 0;
        for (k = i + 1; buf[k] != ')'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        if (toWrite == FIRST) {
          first[first_index].value = tmp;
          first[first_index].address = first_index;
          first[first_index].parent = parent_address;
          strcpy(first[first_index].route, first[parent_address].route);
          first[first_index].route_index = first[parent_address].route_index;
          if (left_or_right == LEFT) {
            first[first_index].addRoute('L');
            first[parent_address].left_child = first_index;
          } else {
            first[first_index].addRoute('R');
            first[parent_address].right_child = first_index;
          }
          first_index++;
        } else {
          second[second_index].value = tmp;
          second[second_index].address = second_index;
          second[second_index].parent = parent_address;
          strcpy(second[second_index].route, second[parent_address].route);
          second[second_index].route_index = second[parent_address].route_index;
          if (left_or_right == LEFT) {
            second[second_index].addRoute('L');
            second[parent_address].left_child = second_index;
          } else {
            second[second_index].addRoute('R');
            second[parent_address].right_child = second_index;
          }
          second_index++;
        }
        break;
      } else {
        if (buf[i] == '(')
          calcDepth++;
        else if (buf[i] == ')')
          calcDepth--;
      }
    }

    if (nextParentAddress != -1) {
      if (left + 1 < nextRight - 1) {
        recursive(left + 1, nextRight - 1, nextParentAddress, LEFT);
      }
      if (nextLeft + 1 < right - 1) {
        recursive(nextLeft + 1, right - 1, nextParentAddress, RIGHT);
      }
    }

  } else {
    calcDepth = 0;
    for (int i = 0, k = 0; i <= right; i++) {
      if (buf[i] == '(')
        calcDepth++;
      else if (buf[i] == ')')
        calcDepth--;
      else {
        if (calcDepth == 0 && buf[i] == '[') {
          tmp = 0;
          for (k = i + 1; buf[k] != ']'; k++) {
            tmp = 10 * tmp + buf[k] - '0';
          }
          if (toWrite == FIRST) {
            first[0].value = tmp;
            first[0].address = 0;
            first[0].addRoute('X');
            first_index++;
          } else {
            second[0].value = tmp;
            second[0].address = 0;
            second[0].addRoute('X');
            second_index++;
          }
          nextRight = i - 1;
          nextLeft = k + 1;
          break;
        }
      }
    }

    if (left + 1 < nextRight - 1) {
      recursive(left + 1, nextRight - 1, 0, LEFT);
    }
    if (nextLeft + 1 < right - 1) {
      recursive(nextLeft + 1, right - 1, 0, RIGHT);
    }
  }
}

void outPut(int address) {

  if (third[address].left_child != -1 || third[address].right_child != -1) {
    if (third[address].left_child != -1) {
      printf("(");
      outPut(third[address].left_child);
      printf(")");
    } else {
      printf("()");
    }
    printf("[%d]", third[address].value);
    if (third[address].right_child != -1) {
      printf("(");
      outPut(third[address].right_child);
      printf(")");
    } else {
      printf("()");
    }
  } else {
    printf("()[%d]()", third[address].value);
  }
}

int main() {

  scanf("%s", buf);
  int length;
  for (length = 0; buf[length] != '\\0'; length++)
    ;
  toWrite = FIRST;
  recursive(0, length - 1, -1, NONE);

  scanf("%s", buf);
  for (length = 0; buf[length] != '\\0'; length++)
    ;
  toWrite = SECOND;
  recursive(0, length - 1, -1, NONE);

  int indexTable[1000];

  third[0].value = first[0].value + second[0].value;
  indexTable[0] = 0;

  int third_index = 1;

  for (int i = 1; i < first_index; i++) {
    for (int k = 1; k < second_index; k++) {
      if (strCmp(first[i].route, second[k].route)) {

        third[third_index].value = first[i].value + second[k].value;
        third[third_index].first_index = i;
        strcpy(third[third_index].route, first[i].route);
        third[third_index].route_index = first[i].route_index;
        indexTable[i] = third_index;
        third_index++;
      }
    }
  }

  int p_tmp;

  for (int i = 1; i < third_index; i++) {
    p_tmp = indexTable[first[third[i].first_index].parent];
    if (third[i].route[third[i].route_index - 1] == 'R') {
      third[p_tmp].right_child = i;
    } else {
      third[p_tmp].left_child = i;
    }
  }

  outPut(0);
  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

enum TYPE { NONE, FIRST, SECOND, LEFT, RIGHT };

struct Node {
  Node() {
    value = address = -1;
    parent = left_child = right_child = -1;
  }
  ll value, address, parent, left_child, right_child;
};

Node first[1000], second[1000], third[1000];
int first_index = 0, second_index = 0;
char buf[1010];

TYPE toWrite;

void recursive(int left, int right, int parent_address, TYPE left_or_right) {
  int calcDepth, nextRight, nextLeft, tmp, nextParentAddress = -1;
  if (parent_address != -1) {
    calcDepth = 0;
    for (int i = left, k = 0; i <= right; i++) {

      if (calcDepth == 0 && buf[i] == '[') {
        tmp = 0;
        for (k = i + 1; buf[k] != ']'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        if (toWrite == FIRST) {
          if (left_or_right == LEFT) {
            first[first_index].value = tmp;
            first[first_index].address = 2 * parent_address + 1;
            first_index++;
            nextParentAddress = 2 * parent_address + 1;
          } else {
            first[first_index].value = tmp;
            first[first_index].address = 2 * parent_address + 2;
            first_index++;
            nextParentAddress = 2 * parent_address + 2;
          }
        } else {
          if (left_or_right == LEFT) {
            second[second_index].value = tmp;
            second[second_index].address = 2 * parent_address + 1;
            second_index++;
            nextParentAddress = 2 * parent_address + 1;
          } else {
            second[second_index].value = tmp;
            second[second_index].address = 2 * parent_address + 2;
            second_index++;
            nextParentAddress = 2 * parent_address + 2;
          }
        }
        nextRight = i - 1;
        nextLeft = k + 1;
        break;
      } else if (calcDepth == 0 && buf[i] == '(' && buf[i + 1] >= '0' &&
                 buf[i + 1] <= '9') {
        tmp = 0;
        for (k = i + 1; buf[k] != ')'; k++) {
          tmp = 10 * tmp + buf[k] - '0';
        }
        if (toWrite == FIRST) {
          if (left_or_right == LEFT) {
            first[first_index].value = tmp;
            first[first_index].address = 2 * parent_address + 1;
            first_index++;
          } else {
            first[first_index].value = tmp;
            first[first_index].address = 2 * parent_address + 2;
            first_index++;
          }
        } else {
          if (left_or_right == LEFT) {
            second[second_index].value = tmp;
            second[second_index].address = 2 * parent_address + 1;
            second_index++;
          } else {
            second[second_index].value = tmp;
            second[second_index].address = 2 * parent_address + 2;
            second_index++;
          }
        }
        break;
      } else {
        if (buf[i] == '(')
          calcDepth++;
        else if (buf[i] == ')')
          calcDepth--;
      }
    }

    if (nextParentAddress != -1) {
      if (left + 1 < nextRight - 1) {
        recursive(left + 1, nextRight - 1, nextParentAddress, LEFT);
      }
      if (nextLeft + 1 < right - 1) {
        recursive(nextLeft + 1, right - 1, nextParentAddress, RIGHT);
      }
    }

  } else {
    calcDepth = 0;
    for (int i = 0, k = 0; i <= right; i++) {
      if (buf[i] == '(')
        calcDepth++;
      else if (buf[i] == ')')
        calcDepth--;
      else {
        if (calcDepth == 0 && buf[i] == '[') {
          tmp = 0;
          for (k = i + 1; buf[k] != ']'; k++) {
            tmp = 10 * tmp + buf[k] - '0';
          }
          if (toWrite == FIRST) {
            first[0].value = tmp;
            first[0].address = 0;
            first_index++;
          } else {
            second[0].value = tmp;
            second[0].address = 0;
            second_index++;
          }
          nextRight = i - 1;
          nextLeft = k + 1;
          break;
        }
      }
    }

    if (left + 1 < nextRight - 1) {
      recursive(left + 1, nextRight - 1, 0, LEFT);
    }
    if (nextLeft + 1 < right - 1) {
      recursive(nextLeft + 1, right - 1, 0, RIGHT);
    }
  }
}

void outPut(int address) {

  if (third[address].left_child != -1 || third[address].right_child != -1) {
    if (third[address].left_child != -1) {
      printf("(");
      outPut(third[address].left_child);
      printf(")");
    } else {
      printf("()");
    }
    printf("[%lld]", third[address].value);
    if (third[address].right_child != -1) {
      printf("(");
      outPut(third[address].right_child);
      printf(")");
    } else {
      printf("()");
    }
  } else {
    printf("()[%lld]()", third[address].value);
  }
}

int main() {

  scanf("%s", buf);
  int length;
  for (length = 0; buf[length] != '\\0'; length++)
    ;
  toWrite = FIRST;
  recursive(0, length - 1, -1, NONE);

  scanf("%s", buf);
  for (length = 0; buf[length] != '\\0'; length++)
    ;
  toWrite = SECOND;
  recursive(0, length - 1, -1, NONE);

  int third_index = 0, indexTable[1000];

  for (int i = 0; i < 1000; i++)
    indexTable[i] = -1;

  for (int i = 0; i < first_index; i++) {
    for (int k = 0; k < second_index; k++) {
      if (first[i].address == second[k].address) {
        if (first[i].address == 0) {
          third[third_index].address = 0;
          third[third_index].value = first[i].value + second[k].value;
          third[third_index].parent = -1;
          indexTable[0] = 0;
          third_index++;
        } else {
          third[third_index].address = first[i].address;
          third[third_index].value = first[i].value + second[k].value;
          indexTable[first[i].address] = third_index;
          third_index++;
        }
      }
    }
  }

  int p_tmp;

  for (int i = 0; i < third_index; i++) {
    if (third[i].address != 0) {
      p_tmp = indexTable[(third[i].address - 1) / 2];
      third[i].parent = p_tmp;
      if (third[i].address % 2 == 1) {
        third[p_tmp].left_child = i;
      } else {
        third[p_tmp].right_child = i;
      }
    }
  }

  outPut(0);
  printf("\\n");

  return 0;
}