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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vs;
    vector<int> vi;
    REP(i, n) {
      string s;
      int d = 0;
      cin >> s;
      REP(j, s.length()) {
        if (s[j] == '.')
          d++;
      }
      vi.push_back(d);
      vs.push_back("");
      REP(j, d) {
        if (j < d - 1)
          vs[i] += " ";
        if (j == d - 1)
          vs[i] += "+";
      }
      vs[i] += s.substr(d, s.length() - d);
      // cout << vs[i] << endl;
    }
    int b = 0;
    for (int j = 0; j < n - 1; j++) {
      for (int i = j + 1; i < n; i++) {
        if (vi[j] > vi[i])
          break;
        if (vi[j] == vi[i]) {
          for (int k = j + 1; k < i; k++) {
            vs[k][vi[j] - 1] = '|';
          }
          break;
        }
      }
    }
    REP(i, n) { cout << vs[i] << endl; }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vs;
    vector<int> vi;
    REP(i, n) {
      string s;
      int d = 0;
      cin >> s;
      REP(j, s.length()) {
        if (s[j] == '.')
          d++;
      }
      vi.push_back(d);
      vs.push_back("");
      REP(j, d) {
        if (j < d - 1)
          vs[i] += " ";
        if (j == d - 1)
          vs[i] += "+";
      }
      vs[i] += s.substr(d, s.length() - d);
      // cout << vs[i] << endl;
    }
    for (int i = 1; i < 51; i++) {
      int b = -1;
      for (int j = 0; j < n; j++) {
        if (vi[j] == i && b == -1)
          b = j;
        else if (vi[j] == i && b != -1) {
          for (int k = b + 1; k < j; k++) {
            vs[k][i - 1] = '|';
          }
          b = j;
        }
      }
    }
    REP(i, n) { cout << vs[i] << endl; }
  }
}