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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int ans, table[41], n;

struct Info {
  Info(int arg_start, int arg_grade, int arg_tmp) {
    start = arg_start;
    grade = arg_grade;
    tmp = arg_tmp;
  }
  Info() { start = grade = tmp = 0; }
  int grade, tmp, start;
};

void recursive(int index, Info info) {

  if (info.grade == 5) {
    ans = min(ans, info.start);
    return;
  }

  if (index == n + 1)
    return;

  switch (info.grade) {
  case 0: // 5
    if (table[index] + info.tmp < 5) {
      recursive(index + 1, Info(info.start, 0, info.tmp + table[index]));
    } else if (table[index] + info.tmp == 5) {
      recursive(index + 1, Info(info.start, 1, 0));
    } else {
      return;
    }
    break;
  case 1: // 7
    if (table[index] + info.tmp < 7) {
      recursive(index + 1, Info(info.start, 1, info.tmp + table[index]));
    } else if (table[index] + info.tmp == 7) {
      recursive(index + 1, Info(info.start, 2, 0));
    } else {
      return;
    }
    break;
  case 2: // 5
    if (table[index] + info.tmp < 5) {
      recursive(index + 1, Info(info.start, 2, info.tmp + table[index]));
    } else if (table[index] + info.tmp == 5) {
      recursive(index + 1, Info(info.start, 3, 0));
    } else {
      return;
    }
    break;
  case 3: // 7
    if (table[index] + info.tmp < 7) {
      recursive(index + 1, Info(info.start, 3, info.tmp + table[index]));
    } else if (table[index] + info.tmp == 7) {
      recursive(index + 1, Info(info.start, 4, 0));
    } else {
      return;
    }
    break;
  case 4: // 7
    if (table[index] + info.tmp < 7) {
      recursive(index + 1, Info(info.start, 4, info.tmp + table[index]));
    } else if (table[index] + info.tmp == 7) {
      recursive(index + 1, Info(info.start, 5, 0));
    } else {
      return;
    }
    break;
  }
}

void func() {
  int length;
  char tmp[11];

  for (int loop = 1; loop <= n; loop++) {
    scanf("%s", tmp);

    for (length = 0; tmp[length] != '\\0'; length++)
      ;

    table[loop] = length;
  }

  for (int i = 1; i <= n - 4; i++) {
    recursive(i, Info(i, 0, 0));
  }

  printf("%d\\n", ans);
}

int main() {

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    ans = 1000000;
    func();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b, c, d, e, j;
  string w[40];
  while (true) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    // cout << "n="<<n << endl;
    for (int i = 0; i < n - 4; i++) {
      // start w[i] 5 7 5 7 7
      // cout << i << "=i" <<endl;
      a = 5;
      b = 7;
      c = 5;
      d = 7;
      e = 7;

      int j = i;
      bool flag = true;
      while (flag) { // 5

        if (a - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (a - w[j].size() > 0) {
          a -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 7

        if (b - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (b - w[j].size() > 0) {
          b -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 5

        if (c - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (c - w[j].size() > 0) {
          c -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 7

        if (d - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (d - w[j].size() > 0) {
          d -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout <<  j << endl;
      while (flag) { // 7

        if (e - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (e - w[j].size() > 0) {
          e -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      if (flag) {
        cout << i + 1 << endl;
        break;
      }
    }
  }

  return 0;
}