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

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

#define MAX 1200

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }

    vector<string> input(n);
    vector<int> index(n);
    vector<vector<int>> nofdot(MAX);
    for (int i = 0; i < n; i++) {
      cin >> input[i];
      for (size_t j = 0; j < input[i].size(); j++)
        if (input[i][j] != '.') {
          index[i] = j;
          nofdot[j].push_back(i);
          break;
        }
    }

    vector<vector<char>> outputMap(n, vector<char>(MAX, '0'));
    for (int i = 1; i < MAX; i++) {
      if (nofdot[i].size() >= 1)
        outputMap[nofdot[i][0]][i - 1] = '+';

      for (size_t j = 1; j < nofdot[i].size(); j++) {
        int start = nofdot[i][j - 1];
        int end = nofdot[i][j];
        outputMap[end][i - 1] = '+';
        for (int k = start + 1; k < end; k++) {
          outputMap[k][i - 1] = '|';
        }
      }
    }

    for (int i = 0; i < n; i++) {
      bool flag = false;
      for (int j = MAX - 1; j >= 0; j--) {
        if (outputMap[i][j] == '+') {
          flag = true;
        } else if (!flag && outputMap[i][j] == '|') {
          outputMap[i][j] = '0';
        } else if (flag && outputMap[i][j] == '0') {
          outputMap[i][j] = ' ';
        }
      }
    }

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < MAX; j++) {
        if (outputMap[i][j] == '|' && outputMap[i - 1][j] != '|' &&
            outputMap[i - 1][j] != '+') {
          outputMap[i][j] = ' ';
        }
      }
    }
    for (int i = n - 2; i >= 0; i--) {
      for (int j = 0; j < MAX; j++) {
        if (outputMap[i][j] == '|' && outputMap[i + 1][j] != '|' &&
            outputMap[i + 1][j] != '+') {
          outputMap[i][j] = ' ';
        }
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; outputMap[i][j] != '0'; j++) {
        cout << outputMap[i][j];
      }
      for (int j = index[i]; j < (int)input[i].size(); j++) {
        cout << input[i][j];
      }
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define MAX 120

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }

    vector<string> input(n);
    vector<int> index(n);
    vector<vector<int>> nofdot(MAX);
    for (int i = 0; i < n; i++) {
      cin >> input[i];
      for (size_t j = 0; j < input[i].size(); j++)
        if (input[i][j] != '.') {
          index[i] = j;
          nofdot[j].push_back(i);
          break;
        }
    }

    vector<vector<char>> outputMap(n, vector<char>(MAX, '0'));
    for (int i = 1; i < MAX; i++) {
      if (nofdot[i].size() >= 1)
        outputMap[nofdot[i][0]][i - 1] = '+';

      for (size_t j = 1; j < nofdot[i].size(); j++) {
        int start = nofdot[i][j - 1];
        int end = nofdot[i][j];
        outputMap[end][i - 1] = '+';
        for (int k = start + 1; k < end; k++) {
          outputMap[k][i - 1] = '|';
        }
      }
    }

    for (int i = 0; i < n; i++) {
      bool flag = false;
      for (int j = MAX - 1; j >= 0; j--) {
        if (outputMap[i][j] == '+') {
          flag = true;
        } else if (!flag && outputMap[i][j] == '|') {
          outputMap[i][j] = '0';
        } else if (flag && outputMap[i][j] == '0') {
          outputMap[i][j] = ' ';
        }
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; outputMap[i][j] != '0'; j++) {
        cout << outputMap[i][j];
      }
      for (int j = index[i]; j < (int)input[i].size(); j++) {
        cout << input[i][j];
      }
      cout << endl;
    }
  }

  return 0;
}