Problem p04030 (C++)

Buggy submission (status: Time Limit Exceeded):
// プログラミングコンテスト用テンプレート
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;

double EPS = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define int(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define lint(x)                                                                \
  long long x;                                                                 \
  scanf("%lld", &x);
#define vint(v, n)                                                             \
  vector<int> v(n);                                                            \
  rep(i, n) scanf("%d", &v[i]);
#define vlint(v, n)                                                            \
  vector<lint> v(n);                                                           \
  rep(i, n) scanf("%lld", &v[i]);
#define double(x)                                                              \
  double x;                                                                    \
  scanf("%lf", &x);
#define vdouble(v, n)                                                          \
  vector<double> v(n);                                                         \
  rep(i, n) scanf("%lf", &v[i]);
#define string(x)                                                              \
  string x;                                                                    \
  cin >> x;

int main() {
  string s, result;
  cin >> s;
  for (int i = 0; i < strlen(s.c_str()); i++) {
    if (s[i] != 'B') {
      result += s[i];
    } else if (s[i] == 'B' && strlen(result.c_str()) != 0) {
      result.pop_back();
    }
  }

  cout << result << endl;
  while (true) {
  }
  return 0;
}

Fixed submission:
// プログラミングコンテスト用テンプレート
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;

double EPS = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define int(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define lint(x)                                                                \
  long long x;                                                                 \
  scanf("%lld", &x);
#define vint(v, n)                                                             \
  vector<int> v(n);                                                            \
  rep(i, n) scanf("%d", &v[i]);
#define vlint(v, n)                                                            \
  vector<lint> v(n);                                                           \
  rep(i, n) scanf("%lld", &v[i]);
#define double(x)                                                              \
  double x;                                                                    \
  scanf("%lf", &x);
#define vdouble(v, n)                                                          \
  vector<double> v(n);                                                         \
  rep(i, n) scanf("%lf", &v[i]);
#define string(x)                                                              \
  string x;                                                                    \
  cin >> x;

int main() {
  string s, result;
  cin >> s;
  for (int i = 0; i < strlen(s.c_str()); i++) {
    if (s[i] != 'B') {
      result += s[i];
    } else if (s[i] == 'B' && strlen(result.c_str()) != 0) {
      result.pop_back();
    }
  }

  cout << result << endl;

  return 0;
}