Problem p04030 (C++)

Buggy submission (status: Runtime Error):
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define RFOR(i, j, n) for (int i = j - 1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v)                                                         \
  REP(__k, v.size()) {                                                         \
    cout << v[__k];                                                            \
    if (__k != v.size() - 1)                                                   \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, st = "";
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == '0')
      st.push_back('0');
    if (s[i] == '1')
      st.push_back('1');
    if (s[i] == 'B')
      st.pop_back();
  }
  cout << st << endl;
  return 0;
}
// templete by private_yusuke

Fixed submission:
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define RFOR(i, j, n) for (int i = j - 1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v)                                                         \
  REP(__k, v.size()) {                                                         \
    cout << v[__k];                                                            \
    if (__k != v.size() - 1)                                                   \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, st = "";
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == '0')
      st.push_back('0');
    if (s[i] == '1')
      st.push_back('1');
    if (s[i] == 'B' && !(st.empty()))
      st.pop_back();
  }
  cout << st << endl;
  return 0;
}
// templete by private_yusuke