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

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    vector<int> v(n);
    REP(i, n) cin >> s[i];
    FOR(i, 1, n) {
      REP(j, s[i].size()) {
        if (s[i][j] != '.') {
          s[i][j - 1] = '+';
          v[i] = j - 1;
          break;
        }
      }
    }

    REP(i, s.size()) {
      FOR(j, i + 1, s.size()) {
        if (s[j].size() >= v[i] && v[i] == v[j]) {
          FOR(k, i + 1, j) { s[k][v[j]] = '|'; }
          break;
        }
        if (s[j].size() < v[i] || s[j][v[i]] != '.')
          break;
      }
    }

    REP(i, s.size()) { REP(j, s[i].size()) if (s[i][j] == '.') s[i][j] = ' '; }

    REP(i, s.size()) { cout << s[i] << endl; }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    vector<int> v(n);
    REP(i, n) cin >> s[i];
    FOR(i, 1, n) {
      REP(j, s[i].size()) {
        if (s[i][j] != '.') {
          s[i][j - 1] = '+';
          v[i] = j - 1;
          break;
        }
      }
    }

    REP(i, s.size()) {
      FOR(j, i + 1, s.size()) {
        if (s[j].size() >= v[i] && v[i] == v[j]) {
          FOR(k, i + 1, j) s[k][v[j]] = '|';
          break;
        }
      }
    }

    REP(i, s.size()) { REP(j, s[i].size()) if (s[i][j] == '.') s[i][j] = ' '; }

    REP(i, s.size()) { cout << s[i] << endl; }
  }
  return 0;
}