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

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string i2s(ll x) {
  if (x == 0)
    return "0";
  string y;
  ll k = 1;
  while (x >= k)
    k *= 10;
  k /= 10;
  for (ll i = k; i > 0; i /= 10) {
    y += (x / i) % 10 + '0';
  }
  return y;
}
ll s2i(string x) {
  for (ll i = 0; i < x.size(); i++)
    if (x[i] == '.')
      x[i] = ' ';
  stringstream ss(x);
  ll y;
  ss >> y;
  return y;
}
int main() {
  // cout << i2s(100) << endl;
  ll n;

  while (cin >> n, n) {
    string buf;
    vector<string> v;
    vector<ll> d;
    ll i, j, k;
    for (i = 0; i < n; i++) {
      cin >> buf;
      j = 0;
      while (buf[j] == '.')
        j++;
      v.push_back(buf);
      d.push_back(j);
    }
    while (v.size() != 1) {
      // for(i=0;i<v.size();i++) cout << v[i] << endl;
      // cout << endl;
      // cout << v.size() << endl;
      k = 1;
      for (i = 0; i < v.size(); i++) {
        if (d[k] < d[i])
          k = i;
      }
      // cout << k << ":" << v[k-1][v[k-1].size()-1] << endl;
      if (v[k - 1][v[k - 1].size() - 1] == '+') {
        i = 0;
        j = 0;
        while (1) {
          if (d[k] != d[k + i])
            break;
          // j*=(v[k+i][v[k+i].size()-1]-'0');
          j += s2i(v[k + i]);
          i++;
          if (k + i == d.size())
            break;
        }
        // cout << i << ":" << j << endl;
        v[k - 1] = v[k - 1].substr(0, v[k - 1].size() - 1);
        v[k - 1] += i2s(j);
        v.erase(v.begin() + k, v.begin() + k + i);
        d.erase(d.begin() + k, d.begin() + k + i);
      }
      if (v[k - 1][v[k - 1].size() - 1] == '*') {
        i = 0;
        j = 1;
        while (1) {
          if (d[k] != d[k + i])
            break;
          // j*=(v[k+i][v[k+i].size()-1]-'0');
          j *= s2i(v[k + i]);
          i++;
          if (k + i == d.size())
            break;
        }
        // cout << i << endl;
        v[k - 1] = v[k - 1].substr(0, v[k - 1].size() - 1);
        v[k - 1] += i2s(j);
        v.erase(v.begin() + k, v.begin() + k + i);
        d.erase(d.begin() + k, d.begin() + k + i);
      }
    }
    cout << v[0] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
string i2s(int x) {
  string y;
  int k = 1;
  while (x >= k)
    k *= 10;
  k /= 10;
  for (int i = k; i > 0; i /= 10) {
    y += (x / i) % 10 + '0';
  }
  return y;
}
int s2i(string x) {
  for (int i = 0; i < x.size(); i++)
    if (x[i] == '.')
      x[i] = ' ';
  stringstream ss(x);
  int y;
  ss >> y;
  return y;
}
int main() {
  // cout << i2s(100) << endl;
  int n;

  while (cin >> n, n) {
    string buf;
    vector<string> v;
    vector<int> d;
    int i, j, k;
    for (i = 0; i < n; i++) {
      cin >> buf;
      j = 0;
      while (buf[j] == '.')
        j++;
      v.push_back(buf);
      d.push_back(j);
    }
    while (v.size() != 1) {
      // for(i=0;i<v.size();i++) cout << v[i] << endl;
      // cout << endl;
      // cout << v.size() << endl;
      k = 1;
      for (i = 0; i < v.size(); i++) {
        if (d[k] < d[i])
          k = i;
      }
      // cout << k << ":" << v[k-1][v[k-1].size()-1] << endl;
      if (v[k - 1][v[k - 1].size() - 1] == '+') {
        i = 0;
        j = 0;
        while (d[k] == d[k + i]) {
          // j+=v[k+i][v[k+i].size()-1]-'0';
          j += s2i(v[k + i]);
          i++;
        }
        // cout << c << endl;
        v[k - 1] = v[k - 1].substr(0, v[k - 1].size() - 1);
        v[k - 1] += i2s(j);
        v.erase(v.begin() + k, v.begin() + k + i);
        d.erase(d.begin() + k, d.begin() + k + i);
      }
      if (v[k - 1][v[k - 1].size() - 1] == '*') {
        i = 0;
        j = 1;
        while (d[k] == d[k + i]) {
          // j*=(v[k+i][v[k+i].size()-1]-'0');
          j *= s2i(v[k + i]);
          i++;
        }
        v[k - 1] = v[k - 1].substr(0, v[k - 1].size() - 1);
        v[k - 1] += i2s(j);
        v.erase(v.begin() + k, v.begin() + k + i);
        d.erase(d.begin() + k, d.begin() + k + i);
      }
    }
    cout << v[0] << endl;
  }
  return 0;
}