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

#include <utility> // pair make_pair
using std::make_pair;
using std::pair;
using std::swap; // c++11
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::make_tuple;
using std::tuple; // get<address>(variable)
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::stoi;
using std::stoll;
using std::string;
using std::to_string;
#include <queue> // queue priority_queue
using std::priority_queue;
using std::queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set>          // set
using std::set;         // insert erase find count
#include <algorithm>    // min max
using std::lower_bound; // iterator,  >=
using std::max;
using std::max_element; // iterator
using std::min;
using std::min_element; // iterator
using std::sort;
using std::upper_bound; // iterator,  >
#include <iostream>     // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <complex>  // complex
using std::complex; // real imag

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    Vec<pair<int, string>> table(n);
    int max_count = 0;
    FOR(i, 0, n) {
      string str;
      cin >> str;
      int count = 0;
      int len = str.length();
      int j = 0;
      while (1) {
        if (str[j] == '.') {
          count++;
          j++;
        } else {
          break;
        }
      }
      string rest = str.substr(count, len - count);
      table[i] = make_pair(count, rest);
      max_count = max(max_count, count);
    }

    Vec<Vec<char>> output(n, Vec<char>(max_count + 1, '.'));
    FOR(i, 0, n) { output[i][table[i].first] = '+'; }
    FOR(j, 0, max_count + 1) {
      // cout << "J = " << j << endl;
      int start = -1;
      FOR(i, 0, n) {
        if (output[i][j] == '+') {
          if (start == -1) {
            start = i;
          } else {
            FOR(k, start, i) {
              if (output[k][j] == '.') {
                output[k][j] = '|';
              }
            }
            start = i;
          }
        } else if (j >= table[i].first) {
          start = -1;
        }
        // cout << i << " " << start << " " << end << endl;
      }
    }

    cout << table[0].second << endl;
    FOR(i, 1, n) {
      int j = 1;
      while (1) {
        if (output[i][j] == '.') {
          cout << ' ';
        } else {
          cout << output[i][j];
        }
        if (output[i][j] == '+') {
          break;
        }
        j++;
      }
      cout << table[i].second << endl;
      ;
    }
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <utility> // pair make_pair
using std::make_pair;
using std::pair;
using std::swap; // c++11
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::make_tuple;
using std::tuple; // get<address>(variable)
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::stoi;
using std::stoll;
using std::string;
using std::to_string;
#include <queue> // queue priority_queue
using std::priority_queue;
using std::queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set>          // set
using std::set;         // insert erase find count
#include <algorithm>    // min max
using std::lower_bound; // iterator,  >=
using std::max;
using std::max_element; // iterator
using std::min;
using std::min_element; // iterator
using std::sort;
using std::upper_bound; // iterator,  >
#include <iostream>     // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <complex>  // complex
using std::complex; // real imag

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    Vec<pair<int, string>> table(n);
    int max_count = 0;
    FOR(i, 0, n) {
      string str;
      cin >> str;
      int count = 0;
      int len = str.length();
      int j = 0;
      while (1) {
        if (str[j] == '.') {
          count++;
          j++;
        } else {
          break;
        }
      }
      string rest = str.substr(count, len - count);
      table[i] = make_pair(count, rest);
      max_count = max(max_count, count);
    }
    Vec<Vec<char>> output(n, Vec<char>(max_count, '.'));
    FOR(i, 0, n) { output[i][table[i].first - 1] = '+'; }
    FOR(j, 0, max_count) {
      int start = -1;
      int end = -1;
      FOR(i, 0, n) {
        if (output[i][j] == '+') {
          if (start == -1) {
            start = i;
          } else {
            end = i;
          }
        } else if (output[i][j] != '.') {
          if (end != -1) {
            FOR(i, start, end) {
              if (output[i][j] == '.') {
                output[i][j] = '|';
              }
            }
          }
          start = -1;
          end = -1;
        }
      }
      if (end != -1) {
        FOR(i, start, end) {
          if (output[i][j] == '.') {
            output[i][j] = '|';
          }
        }
      }
    }

    cout << table[0].second << endl;
    FOR(i, 1, n) {
      int j = 0;
      while (1) {
        if (output[i][j] == '.') {
          cout << ' ';
        } else {
          cout << output[i][j];
        }
        if (output[i][j] == '+') {
          break;
        }
        j++;
      }
      cout << table[i].second << endl;
      ;
    }
  }
  return 0;
}