alexjercan/bugnet huggingface dataset:
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 100
using namespace std;
string s[N];
int n, y;

int dfs(int x) {
  int res;
  if ('0' <= s[y][x] && s[y][x] <= '9')
    return s[y][x] - '0';
  if (s[y][x] == '+') {
    res = 0;
    while (x + 2 <= s[y + 1].size())
      y++, res += dfs(x + 1);
  }
  if (s[y][x] == '*') {
    res = 1;
    while (x + 2 <= s[y + 1].size())
      y++, res *= dfs(x + 1);
  }
  return res;
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < N; i++)
      s[i] = "";
    for (int i = 0; i < n; i++)
      cin >> s[i];
    y = 0;
    cout << dfs(0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 11
using namespace std;
string s[N];
int n, y;

int dfs(int x) {
  int res;
  if ('0' <= s[y][x] && s[y][x] <= '9')
    return s[y][x] - '0';
  if (s[y][x] == '+') {
    res = 0;
    while (x + 2 <= s[y + 1].size())
      y++, res += dfs(x + 1);
  }
  if (s[y][x] == '*') {
    res = 1;
    while (x + 2 <= s[y + 1].size())
      y++, res *= dfs(x + 1);
  }
  return res;
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    y = 0;
    cout << dfs(0) << endl;
  }
  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int N;
vector<string> S;
int cur;

int dfs(int depth) {
  char c = S[cur][depth];
  if (isdigit(c)) {
    cur++;
    return c - '0';
  }
  if (c == '+') {
    int s = 0;
    cur++;
    while (cur < N && depth < S[cur].size() && S[cur][depth] == '.') {
      s += dfs(depth + 1);
    }
    return s;
  }
  if (c == '*') {
    int p = 1;
    cur++;
    while (cur < N && depth < S[cur].size() && S[cur][depth] == '.') {
      p *= dfs(depth + 1);
    }
    return p;
  }

  return -1;
}

int main() {
  while (cin >> N, N) {
    S.resize(N);
    for (auto &s : S) {
      cin >> s;
    }

    cur = 0;
    cout << dfs(0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N;
vector<string> S;
int cur;

int dfs(int depth) {
  char c = S[cur][depth];
  if (isdigit(c)) {
    cur++;
    return c - '0';
  }
  if (c == '+') {
    int s = 0;
    cur++;
    while (cur < N && S[cur][depth] == '.') {
      s += dfs(depth + 1);
    }
    return s;
  }
  if (c == '*') {
    int p = 1;
    cur++;
    while (cur < N && S[cur][depth] == '.') {
      p *= dfs(depth + 1);
    }
    return p;
  }

  return -1;
}

int main() {
  while (cin >> N, N) {
    S.resize(N);
    for (auto &s : S)
      cin >> s;

    cur = 0;
    cout << dfs(0) << endl;
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ALL(a) (a).begin(), (a).end()
#define llong long long

using namespace std;
using strs = vector<string>;
int mlt(int &idx, int d);
int add(int &idx, int d);
int inum(int &idx, int d);
strs str;

int mlt(int &idx, int d) {
  int ret = 1;
  for (; idx < str.size() && d < str[idx].size(); idx++) {
    if (str[idx][d] == '*') {
      ret *= mlt(++idx, d + 1);
      --idx;
    } else if (str[idx][d] == '+') {
      ret *= add(++idx, d + 1);
      --idx;
    } else if (isdigit(str[idx][d])) {
      ret *= inum(idx, d);
    } else {
      cout << idx << ":" << d << endl;
      assert(0);
    }
  }
  return ret;
}

int add(int &idx, int d) {
  int ret = 0;
  for (; idx < str.size() && d < str[idx].size(); idx++) {
    //		cerr << idx << ":" << str[idx][d] << " ";
    if (str[idx][d] == '*') {
      ret += mlt(++idx, d + 1);
      --idx;
    } else if (str[idx][d] == '+') {
      ret += add(++idx, d + 1);
      --idx;
    } else if (isdigit(str[idx][d])) {
      ret += inum(idx, d);
    } else {
      cout << idx << ":" << d << endl;
      assert(0);
    }
  }
  return ret;
}

int inum(int &idx, int d) {
  //	cerr << idx << ":" << d << endl;
  //	cerr << str[idx].substr(d) << endl;
  return stoi(str[idx].substr(d));
}

signed main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    str = strs(n);
    for (auto &e : str)
      cin >> e;
    int idx = 0;
    cout << add(idx, 0) << endl;
    //		cerr << "\\n\\n" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int add(vector<string>::iterator &itr);
int mlt(vector<string>::iterator &itr);

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int ans = 0;
    for (vector<string>::iterator itr = s.begin(); itr != s.end(); ++itr) {
      if ((itr->back()) == '+') {
        ans += add(itr);
      } else if ((itr->back()) == '*') {
        ans += mlt(itr);
      } else {
        ans += itr->back() - '0';
      }
    }
    cout << ans << endl;
  }

  return 0;
}

int add(vector<string>::iterator &itr) {
  int c = itr->size() + 1;
  int ret = 0;
  ++itr;
  while ((int)(itr->size()) == c) {
    if (itr->back() == '+') {
      ret += add(itr);
    } else if ((itr->back()) == '*') {
      ret += mlt(itr);
    } else {
      ret += itr->back() - '0';
    }
    ++itr;
  }

  --itr;
  return ret;
}

int mlt(vector<string>::iterator &itr) {
  int c = itr->size() + 1;
  int ret = 1;
  ++itr;
  while ((int)(itr->size()) == c) {
    if (itr->back() == '+') {
      ret *= add(itr);
    } else if ((char)(itr->back()) == '*') {
      ret *= mlt(itr);
    } else {
      ret *= itr->back() - '0';
    }
    ++itr;
  }
  --itr;
  return ret;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int n;
string s[50];
int calc(int a, int b, bool m) {
  int res = m;
  for (int i = b; i < n; i++) {
    if (s[i].size() <= a)
      break;
    if ('0' <= s[i][a] && s[i][a] <= '9') {
      if (m)
        res *= s[i][a] - '0';
      else
        res += s[i][a] - '0';
    }
    if (s[i][a] == '*') {
      if (m)
        res *= calc(a + 1, i + 1, 1);
      else
        res += calc(a + 1, i + 1, 1);
    }
    if (s[i][a] == '+') {
      if (m)
        res *= calc(a + 1, i + 1, 0);
      else
        res += calc(a + 1, i + 1, 0);
    }
  }
  return res;
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    printf("%d\\n", calc(0, 0, 0));
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int n;
string s[10];
int calc(int a, int b, bool m) {
  int res = m;
  for (int i = b; i < n; i++) {
    if (s[i].size() <= a)
      break;
    if ('0' <= s[i][a] && s[i][a] <= '9') {
      if (m)
        res *= s[i][a] - '0';
      else
        res += s[i][a] - '0';
    }
    if (s[i][a] == '*') {
      if (m)
        res *= calc(a + 1, i + 1, 1);
      else
        res += calc(a + 1, i + 1, 1);
    }
    if (s[i][a] == '+') {
      if (m)
        res *= calc(a + 1, i + 1, 0);
      else
        res += calc(a + 1, i + 1, 0);
    }
  }
  return res;
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) cin >> s[i];
    printf("%d\\n", calc(0, 0, 0));
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  static auto add = [](int a, int b) { return a + b; };
  static auto mul = [](int a, int b) { return a * b; };
  int n;
  while (cin >> n, n) {
    vector<string> a(n);
    for (auto &e : a) {
      cin >> e;
    }
    a.push_back("0");
    vector<pair<function<int(int, int)>, int>> v{make_pair(add, 0)};
    for (auto e : a) {
      while (e.size() - 1 < v.size() - 1) {
        (v.rbegin() + 1)->second =
            (v.rbegin() + 1)
                ->first((v.rbegin() + 1)->second, v.rbegin()->second);
        v.pop_back();
      }
      char c = *e.rbegin();
      if (c == '+') {
        v.push_back(make_pair(add, 0));
      } else if (c == '*') {
        v.push_back(make_pair(mul, 1));
      } else {
        v.rbegin()->second = v.rbegin()->first(v.rbegin()->second, c - '0');
      }
    }
    cout << v.back().second << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  static auto add = [](int a, int b) { return a + b; };
  static auto mul = [](int a, int b) { return a * b; };
  int n;
  while (cin >> n, n) {
    vector<string> a(n);
    for (auto &e : a) {
      cin >> e;
    }
    a.push_back("0");
    vector<pair<function<int(int, int)>, int>> v{make_pair(add, 0)};
    for (auto e : a) {
      while (e.size() - 1 < v.size() - 1) {
        (v.rbegin() - 1)->second =
            (v.rbegin() - 1)
                ->first((v.rbegin() - 1)->second, v.rbegin()->second);
        v.pop_back();
      }
      char c = *e.rbegin();
      if (c == '+') {
        v.push_back(make_pair(add, 0));
      } else if (c == '*') {
        v.push_back(make_pair(mul, 1));
      } else {
        v.rbegin()->second = v.rbegin()->first(v.rbegin()->second, c - '0');
      }
    }
    cout << v.back().second << endl;
  }
  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <iostream>
#include <stack>
using namespace std;

char o[200];
int d[200], n, j;
string s;
int x(int _) {
  if (o[j] >= '0' && o[j] <= '9')
    return o[j++] - '0';
  int k = j, r;
  o[j++] == '*' ? r = 1 : r = 0;
  while (d[k] + 1 == d[j])
    if (o[k] == '*')
      r *= x(j);
    else
      r += x(j);
  return r;
}
int main() {
  while (cin >> n, n) {
    d[n] = 0;
    for (int i = 0; i < n; i++) {
      cin >> s;
      o[i] = s[s.length() - 1];
      d[i] = s.length();
    }
    j = 0;
    cout << x(0) << endl;
  }
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <stack>
using namespace std;

int n;
stack<pair<char, int>> ope;
stack<pair<int, int>> st;
void solve(int level) {
  while (st.top().second > level) {
    char op = ope.top().first;
    ope.pop();
    stack<int> sta;
    int l = st.top().second;
    while (st.size() && st.top().second == l) {
      sta.push(st.top().first);
      st.pop();
    }
    int th = sta.top();
    sta.pop();
    while (sta.size()) {
      if (op == '+') {
        th += sta.top();
      } else {
        th *= sta.top();
      }
      sta.pop();
    }
    st.push(make_pair(th, l - 1));
  }
}
int main() {
  int ca = 0;
  while (cin >> n, n) {
    while (ope.size())
      ope.pop();
    while (st.size())
      st.pop();
    if (ca++ > 100)
      return -1;
    if (n == 1) {
      cin >> n;
      cout << n << endl;
      continue;
    }
    char c;
    int i, level;
    for (int tt = 0; tt < n; tt++) {
      cin >> c;
      level = 0;
      while (c == '.') {
        level++;
        cin >> c;
      }
      if (c >= '0' && c <= '9') {
        i = c - '0';
        if (st.size())
          solve(level);
        st.push(make_pair(i, level));
      } else
        ope.push(make_pair(c, level));
    }
    solve(0);
    cout << st.top().first << endl;
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, row;
vector<string> str;

int operand(int level) {
  int answer = 0;
  switch (str[row][level]) {
  case '+':
    row++, level++;
    answer = 0;
    while (row < n) {
      if (str[row].size() < level || str[row][level - 1] != '.')
        break;
      answer += operand(level);
    }
    break;

  case '*':
    row++, level++;
    answer = 1;
    while (row < n) {
      if (str[row].size() < level || str[row][level - 1] != '.')
        break;
      answer *= operand(level);
    }
    break;

  default:
    answer = stoi(str[row].substr(level));
    row++;
  }
  return answer;
}

int main() {
  while (cin >> n && n != 0) {
    row = 0;
    str = vector<string>(n);
    for (int i = 0; i < n; i++)
      cin >> str[i];
    cout << operand(0) << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, row;
vector<string> str;

int operand(int level) {
  int answer = 0;
  switch (str[row][level]) {
  case '+':
    row++, level++;
    answer = 0;
    while (row < n) {
      if (str[row][level - 1] != '.')
        break;
      answer += operand(level);
    }
    break;

  case '*':
    row++, level++;
    answer = 1;
    while (row < n) {
      if (str[row][level - 1] != '.')
        break;
      answer *= operand(level);
    }
    break;

  default:
    answer = stoi(str[row].substr(level));
    row++;
  }
  return answer;
}

int main() {
  while (cin >> n && n != 0) {
    row = 0;
    str = vector<string>(n);
    for (int i = 0; i < n; i++)
      cin >> str[i];
    cout << operand(0) << endl;
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool solve() {
  int n;
  cin >> n;
  if (n == 0)
    return false;
  vector<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  reverse(s.begin(), s.end());
  vector<long long> v;
  int indent = 0;
  for (int i = 0; i < n; i++) {
    int ind = 0;
    for (; s[i][ind] == '.'; ind++)
      ;
    if (indent < ind) {
      for (int j = 0; j < ind - indent; j++)
        v.push_back(-1);
    }
    indent = ind;

    if (s[i][ind] == '+' || s[i][ind] == '*') {
      long long ret = (s[i][ind] == '+') ? 0 : 1;
      while (v[v.size() - 1] >= 0) {
        if (s[i][ind] == '+') {
          ret += v[v.size() - 1];
        } else {
          ret *= v[v.size() - 1];
        }
        v.pop_back();
      }
      v.pop_back();
      v.push_back(ret);
    } else {
      v.push_back(s[i][ind] - '0');
    }
  }
  cout << v[0] << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool solve() {
  int n;
  cin >> n;
  if (n == 0)
    return false;
  vector<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  reverse(s.begin(), s.end());
  vector<long long> v;
  int indent = 0;
  for (int i = 0; i < n; i++) {
    int ind = 0;
    for (; s[i][ind] == '.'; ind++)
      ;
    if (indent < ind) {
      for (int j = 0; j < ind - indent; j++)
        v.push_back(-1);
      indent = ind;
    }
    if (s[i][ind] == '+' || s[i][ind] == '*') {
      long long ret = (s[i][ind] == '+') ? 0 : 1;
      while (v[v.size() - 1] >= 0) {
        if (s[i][ind] == '+') {
          ret += v[v.size() - 1];
        } else {
          ret *= v[v.size() - 1];
        }
        v.pop_back();
      }
      v.pop_back();
      v.push_back(ret);
    } else {
      v.push_back(s[i][ind] - '0');
    }
  }
  cout << v[0] << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<int, string>> vpis;
int parse(const vpis &v, int l, int r) {
  if (v[l].second != "+" && v[l].second != "*")
    return strtol(v[l].second.c_str(), NULL, 10);
  int unit = v[l].second == "+" ? 0 : 1;
  for (int i = l + 1; i < r;) {
    int j = i + 1;
    if (v[i].second == "+" || v[i].second == "*") {
      for (; j < v.size() && v[j].first > v[i].first; j++)
        ;
    }
    int n = parse(v, i, j);
    i = j;
    if (v[l].second == "+")
      unit += n;
    else
      unit *= n;
  }
  return unit;
}
int main() {
  int N;
  string s;
  for (; cin >> N, N;) {
    vpis v(N);
    for (int i = 0; i < N; i++) {
      cin >> s;
      int idx = 0;
      for (; s[idx] == '.'; idx++)
        ;
      v[i] = make_pair(idx, s.substr(idx));
    }
    printf("%d\\n", parse(v, 0, N));
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<int, string>> vpis;
int parse(const vpis &v, int l, int r) {
  if (v[l].second != "+" && v[l].second != "*")
    return strtol(v[l].second.c_str(), NULL, 10);
  int unit = v[l].second == "+" ? 0 : 1;
  for (int i = l + 1; i < r;) {
    int j = i + 1;
    if (v[i].second == "+" || v[i].second == "*") {
      for (; v[j].first > v[i].first; j++)
        ;
    }
    int n = parse(v, i, j);
    i = j;
    if (v[l].second == "+")
      unit += n;
    else
      unit *= n;
  }
  return unit;
}
int main() {
  int N;
  string s;
  for (; cin >> N, N;) {
    vpis v(N);
    for (int i = 0; i < N; i++) {
      cin >> s;
      int idx = 0;
      for (; s[idx] == '.'; idx++)
        ;
      v[i] = make_pair(idx, s.substr(idx));
    }
    printf("%d\\n", parse(v, 0, N));
  }
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int calc(vector<string> s, int &i) {
  int depth = s[i].length(), n = s.size();
  char c = s[i][depth - 1];
  if (c == '+') {
    int res = 0;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res += calc(s, i);
    }
    return res;
  } else if (c == '*') {
    int res = 1;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res *= calc(s, i);
    }
    return res;
  } else {
    i++;
    return c - '0';
  }
}

int main() {
  int n;
  ifstream ifs("in.txt");
  while (cin >> n && n) {
    vector<string> s(n + 1);
    REP(i, n) cin >> s[i];
    int i = 0;
    cout << calc(s, i) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi acos(-1.0)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int calc(vector<string> s, int &i) {
  int depth = s[i].length(), n = s.size();
  char c = s[i][depth - 1];
  if (c == '+') {
    int res = 0;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res += calc(s, i);
    }
    return res;
  } else if (c == '*') {
    int res = 1;
    i++;
    while (s[i].length() == depth + 1 && i < n) {
      res *= calc(s, i);
    }
    return res;
  } else {
    i++;
    return c - '0';
  }
}

int main() {
  int n;
  ifstream ifs("in.txt");
  while (cin >> n && n) {
    vector<string> s(n);
    REP(i, n) cin >> s[i];
    int i = 0;
    cout << calc(s, i) << endl;
  }

  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
string s[30];
int dfs(int d, int k, int e) {
  int res = 0;
  if (e)
    res = 1;
  for (int i = d; i < n && k < s[i].size(); i++) {
    if (s[i][k] == '.')
      continue;
    int a = s[i][k] - '0';
    if (s[i][k] == '+')
      a = dfs(i + 1, k + 1, 0);
    if (s[i][k] == '*')
      a = dfs(i + 1, k + 1, 1);
    if (e)
      res *= a;
    else
      res += a;
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
string s[11];
int dfs(int d, int k) {
  bool e = 0;
  int res = 0;
  if (s[d][k] == '+')
    d++, k++;
  if (s[d][k] == '*')
    e = 1, d++, k++, res = 1;
  for (int i = d; i < n && k < s[i].size(); i++) {
    int a = s[i][k] - '0';
    if (s[i][k] == '.')
      continue;
    if (s[i][k] == '+' || s[i][k] == '*')
      a = dfs(i, k);
    if (e)
      res *= a;
    else
      res += a;
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];
    cout << dfs(0, 0) << endl;
  }
  return 0;
}
Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n;
int cursor;
vector<string> s;

int calc(int mode, int depth) {
  int sum = mode == 1 ? 0 : 1;

  while (cursor < n && depth == s[cursor].size() - 1) {
    string str = s[cursor];
    int points = str.size() - 1;
    char a = str[points];
    int num = a - '0';
    if (mode == 1) {
      if (a == '+') {
        cursor++;
        sum += calc(1, depth + 1);
      } else if (a == '*') {
        cursor++;
        sum += calc(2, depth + 1);
      } else {
        cursor++;
        sum += num;
      }
    }
    if (mode == 2) {
      if (a == '+') {
        cursor++;
        sum *= calc(1, depth + 1);
      } else if (a == '*') {
        cursor++;
        sum *= calc(2, depth + 1);
      } else {
        cursor++;
        sum *= num;
      }
    }
  }
  return sum;
}

int main() {
  while (cin >> n, n != 0) {
    s = vector<string>(n);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    cursor = 0;
    int sum = calc(1, 0);
    cout << sum << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int INF = 999999;

int main() {
  int correct[5] = {5, 7, 5, 7, 7};
  int n;

  while (cin >> n, n != 0) {
    vector<string> s;
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      s.push_back(str);
    }
    for (int i = 0;; i++) {
      int j = i;
      int rem = 5;
      while (rem > 0) {
        rem -= s[j].size();
        j++;
      }
      if (rem != 0)
        continue;

      rem = 7;
      while (rem > 0) {
        rem -= s[j].size();
        j++;
      }
      if (rem != 0)
        continue;
      rem = 5;
      while (rem > 0) {
        rem -= s[j].size();
        j++;
      }
      if (rem != 0)
        continue;
      rem = 7;
      while (rem > 0) {
        rem -= s[j].size();
        j++;
      }
      if (rem != 0)
        continue;
      rem = 7;
      while (rem > 0) {
        rem -= s[j].size();
        j++;
      }
      if (rem != 0)
        continue;
      cout << i + 1 << endl;
      break;
    }
  }
  return 0;
}