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

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n;
    int maxi = 0;
    bool flag = false;

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

      if (maxi < count[i]) {
        maxi = count[i];
      }
    }

    FOR(i, 0, maxi - 1) {
      FOR(j, 1, n) {
        flag = false;

        FOR(k, j + 1, n) {
          // cout << j << "  " << k << endl;
          if (count[j] > count[k]) {
            break;
          }
          if (count[j] == count[k]) {
            s[j][count[j] - 1] = '+';
            s[k][count[k] - 1] = '+';
            FOR(l, j + 1, k - 1) {
              // cout << l << endl;
              if (count[j] - 1 <= s[l].size()) {
                if (s[l][count[j] - 1] == '.') {
                  s[l][count[j] - 1] = '|';
                }
              }
            }
            flag = true;
            break;
          }
        }

        if (flag == false && count[j] != 0) {
          // cout << j << " " << count[j] - 1 << endl;
          s[j][count[j] - 1] = '+';
        }
      }
    }

    FOR(i, 1, n) {
      FOR(j, 0, count[i] - 1) {
        if (s[i][j] == '.') {
          s[i][j] = ' ';
        }
      }
    }
    FOR(i, 1, n) { cout << s[i] << endl; }
  }
  return 0;
}


Error: WA

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n;
    int maxi = 0;
    bool flag = false;

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

      if (maxi < count[i]) {
        maxi = count[i];
      }
    }

    FOR(i, 0, maxi - 1) {
      FOR(j, 1, n) {
        flag = false;

        FOR(k, j + 1, n) {
          if (count[j] == count[k]) {
            s[j][count[j] - 1] = '+';
            s[k][count[k] - 1] = '+';
            FOR(l, j + 1, k - 1) { s[l][count[j] - 1] = '|'; }
            flag = true;
            break;
          }
        }

        if (flag == false && count[j] != 0) {
          // cout << j << " " << count[j] - 1 << endl;
          s[j][count[j] - 1] = '+';
        }
      }
    }

    /*FOR(i, 1, n) {
            FOR(j, 0, count[i]-1) {
                    if (s[i][j] == '.') {
                            s[i][j] = ' ';
                    }
            }
    }*/
    FOR(i, 1, n) { cout << s[i] << endl; }
  }
  return 0;
}