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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<char> A_c, B_c, C_c, ans_c;
  vector<int> A_l, B_l, C_l, ans_l;
  char c_ = '!';
  int l;
  while (cin >> c_, c_ != '$') {
    cin >> l;
    A_c.push_back(c_);
    A_l.push_back(l);
  }
  c_ = '!';
  while (cin >> c_, c_ != '$') {
    cin >> l;
    B_c.push_back(c_);
    B_l.push_back(l);
  }
  c_ = '!';
  while (cin >> c_, c_ != '$') {
    cin >> l;
    C_c.push_back(c_);
    C_l.push_back(l);
  }
  int a = A_c.size(), b = B_c.size(), c = C_c.size();
  bool g = false;
  for (int i = 0; i < a; ++i) {
    bool f = true;
    for (int j = 0; j < b; ++j) {
      if (g || i + j >= a) {
        f = false;
        break;
      }
      if ((A_c[i + j] != B_c[j]) ||
          (j > 0 && j < b - 1 && A_l[i + j] != B_l[j]) ||
          ((j == 0 || j == b - 1) && A_l[i + j] < B_l[j])) {
        f = false;
        break;
      }
    }
    if (f) {
      g = true;
      if (B_l.size() > 1 && A_l[i] - B_l[0] > 0) {
        ans_c.push_back(A_c[i]);
        ans_l.push_back(A_l[i] - B_l[0]);
      }
      for (int j = 0; j < c; ++j) {
        if (ans_c.size() > 0 && ans_c.back() == C_c[j])
          ans_l.back() += C_l[j];
        else {
          ans_c.push_back(C_c[j]);
          ans_l.push_back(C_l[j]);
        }
      }
      if (A_l[i + b - 1] - B_l.back() > 0) {
        if (ans_c.size() > 0 && ans_c.back() == A_c[i + b - 1])
          ans_l.back() += A_l[i + b - 1] - B_l.back();
        else {
          ans_c.push_back(A_c[i + b - 1]);
          ans_l.push_back(A_l[i + b - 1] - B_l.back());
        }
      }
      i += b - 1;
    } else {
      if (ans_c.size() > 0 && ans_c.back() == A_c[i])
        ans_l.back() += A_l[i];
      else {
        ans_c.push_back(A_c[i]);
        ans_l.push_back(A_l[i]);
      }
    }
  }
  for (int i = 0; i < ans_c.size(); ++i) {
    cout << ans_c[i] << " " << ans_l[i] << " ";
  }
  cout << "$" << endl;
  return 0;
}


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


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<char> A_c, B_c, C_c, ans_c;
  vector<int> A_l, B_l, C_l, ans_l;
  char c_ = '!';
  int l;
  while (cin >> c_, c_ != '$') {
    cin >> l;
    A_c.push_back(c_);
    A_l.push_back(l);
  }
  c_ = '!';
  while (cin >> c_, c_ != '$') {
    cin >> l;
    B_c.push_back(c_);
    B_l.push_back(l);
  }
  c_ = '!';
  while (cin >> c_, c_ != '$') {
    cin >> l;
    C_c.push_back(c_);
    C_l.push_back(l);
  }
  int a = A_c.size(), b = B_c.size(), c = C_c.size();
  bool g = false;
  for (int i = 0; i < a; ++i) {
    bool f = true;
    for (int j = 0; j < b; ++j) {
      if (g || i + j >= a) {
        f = false;
        break;
      }
      if ((j > 0 && j < b - 1 && A_l[i + j] != B_l[j]) ||
          (A_c[i + j] != B_c[j]) || (j % (b - 1) == 0 && A_l[i + j] < B_l[j])) {
        f = false;
        break;
      }
    }
    if (f) {
      g = true;
      if (A_l[i] - B_l[0] > 0) {
        ans_c.push_back(A_c[i]);
        ans_l.push_back(A_l[i] - B_l[0]);
      }
      for (int j = 0; j < c; ++j) {
        if (ans_c.size() > 0 && ans_c.back() == C_c[j])
          ans_l.back() += C_l[j];
        else {
          ans_c.push_back(C_c[j]);
          ans_l.push_back(C_l[j]);
        }
      }
      if (A_l[i + b - 1] - B_l.back() > 0) {
        if (ans_c.back() == A_c[i + b - 1])
          ans_l.back() += A_l[i + b - 1] - B_l[b - 1];
        else {
          ans_c.push_back(A_c[i + b - 1]);
          ans_l.push_back(A_l[i + b - 1] - B_l[b - 1]);
        }
      }
    } else {
      if (ans_c.back() == A_c[i])
        ans_l.back() += A_l[i];
      else {
        ans_c.push_back(A_c[i]);
        ans_l.push_back(A_l[i]);
      }
    }
    i += b - 1;
  }
  for (int i = 0; i < ans_c.size(); ++i) {
    cout << ans_c[i] << " " << ans_l[i] << " ";
  }
  cout << "$" << endl;
  return 0;
}