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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
vector<pair<char, int>> comp(vector<pair<char, int>> ps) {
  vector<pair<char, int>> newps;
  for (int i = 0; i < ps.size(); ++i) {
    if (i == ps.size() - 1 || ps[i].first != ps[i + 1].first) {
      if (ps[i].second) {
        newps.push_back(ps[i]);
      }
    } else {
      ps[i + 1].second += ps[i].second;
    }
  }
  ps = newps;
  newps.clear();
  for (int i = 0; i < ps.size(); ++i) {
    if (i == ps.size() - 1 || ps[i].first != ps[i + 1].first) {
      if (ps[i].second) {
        newps.push_back(ps[i]);
      }
    } else {
      ps[i + 1].second += ps[i].second;
    }
  }
  return newps;
}
void input(vector<pair<char, int>> &p) {
  while (1) {
    char c;
    cin >> c;
    if (c == '$')
      break;
    int n;
    cin >> n;
    p.push_back(make_pair(c, n));
  }
}
int main() {
  vector<pair<char, int>> a, b, c;
  input(a);
  input(b);
  input(c);
  a = comp(a);
  b = comp(b);
  c = comp(c);
  if (a.size() < b.size()) {

  } else if (b.size() == 1) {
    for (int i = 0; i < a.size(); ++i) {
      if (a[i].first == b[0].first && a[i].second >= b[0].second) {
        a[i].second -= b[0].second;
        a.insert(a.begin() + i, c.begin(), c.end());
        break;
      }
    }
  } else {
    for (int l = 0; l <= a.size() - b.size(); ++l) {
      bool aok = true;
      if (a[l].first != b[0].first || a[l].second < b[0].second) {
        aok = false;
      }
      for (int i = 1; i < b.size() - 1; ++i) {
        if (a[l + i] != b[i]) {
          aok = false;
        }
      }
      if (a[l + b.size() - 1].first != b[b.size() - 1].first ||
          a[l + b.size() - 1].second < b[b.size() - 1].second) {
        aok = false;
      }
      if (aok) {
        a[l].second -= b[0].second;
        a[l + b.size() - 1].second -= b[b.size() - 1].second;
        a.erase(a.begin() + l + 1, a.begin() + l + b.size() - 1);
        a.insert(a.begin() + l + 1, c.begin(), c.end());
        break;
      }
    }
  }
  a = comp(a);
  for (int i = 0; i < a.size(); ++i) {
    cout << a[i].first << " " << a[i].second << " ";
  }
  cout << "$" << endl;
  return 0;
}

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


Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
vector<pair<char, int>> comp(vector<pair<char, int>> ps) {
  vector<pair<char, int>> newps;
  for (int i = 0; i < ps.size(); ++i) {
    if (i == ps.size() - 1 || ps[i].first != ps[i + 1].first) {
      if (ps[i].second) {
        newps.push_back(ps[i]);
      }
    } else {
      ps[i + 1].second += ps[i].second;
    }
  }
  return newps;
}
void input(vector<pair<char, int>> &p) {
  while (1) {
    char c;
    cin >> c;
    if (c == '$')
      break;
    int n;
    cin >> n;
    p.push_back(make_pair(c, n));
  }
}
int main() {
  vector<pair<char, int>> a, b, c;
  input(a);
  input(b);
  input(c);
  a = comp(a);
  b = comp(b);
  c = comp(c);
  if (b.size() == 1) {
    for (int i = 0; i < a.size(); ++i) {
      if (a[i].first == b[0].first && a[i].second >= b[0].second) {
        a[i].second -= b[0].second;
        a.insert(a.begin() + i, c.begin(), c.end());
        break;
      }
    }
  } else {
    for (int l = 0; l <= a.size() - b.size(); ++l) {
      bool aok = true;
      if (a[l].first != b[0].first || a[l].second < b[0].second) {
        aok = false;
      }
      for (int i = 1; i < b.size() - 1; ++i) {
        if (a[l + i] != b[i]) {
          aok = false;
        }
      }
      if (a[l + b.size() - 1].first != b[b.size() - 1].first ||
          a[l + b.size() - 1].second < b[b.size() - 1].second) {
        aok = false;
      }
      if (aok) {
        a[l].second -= b[0].second;
        a[l + b.size() - 1].second -= b[b.size() - 1].second;
        a.erase(a.begin() + l + 1, a.begin() + l + b.size() - 1);
        a.insert(a.begin() + l + 1, c.begin(), c.end());
        break;
      }
    }
  }
  a = comp(a);
  for (int i = 0; i < a.size(); ++i) {
    cout << a[i].first << " " << a[i].second << " ";
  }
  cout << "$" << endl;
  return 0;
}