Problem p01787 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char, ll> P;

vector<P> A, B, C;
void input(vector<P> &a) {
  int num;
  char ch;
  while (cin >> ch, ch != '$') {
    cin >> num;
    a.push_back(P(ch, num));
  }
}

bool check(int i) {
  int n = A.size();
  int m = B.size();
  if (i + m > n)
    return 0;

  if (A[i].first != B[0].first || A[i].second < B[0].second)
    return 0;

  if (m == 1)
    return 1;

  int j = 0;
  for (j++, i++; j < m - 1; j++, i++)
    if (A[i].first != B[j].first || A[i].second != B[j].second)
      return 0;

  if (A[i].first != B[j].first || A[i].second < B[j].second)
    return 0;

  return 1;
}

void mergePush(vector<P> &res, P a) {
  int c = a.second;
  char ch = a.first;
  if (c == 0)
    return;
  if (res.size() && res.back().first == ch)
    res.back().second += c;
  else
    res.push_back(P(ch, c));
}

vector<P> solve() {
  vector<P> res;
  int n = A.size();
  int m = B.size();
  int l = C.size();
  int flg = 0;
  for (int i = 0; i < n; i++) {
    if (!check(i) || flg++) {
      mergePush(res, A[i]);
      continue;
    }
    A[i].second -= B[0].second;
    if (m == 1)
      for (int j = 0; j < l; j++)
        mergePush(res, C[j]);
    else {
      mergePush(res, P(A[i].first, A[i].second));
      for (int j = 0; j < l; j++)
        mergePush(res, C[j]);
      A[i + m - 1].second -= B.back().second;
    }
    i += m - 2;
  }
  return res;
}

int main() {
  input(A), input(B), input(C);
  vector<P> ans = solve();
  for (P a : ans)
    cout << a.first << " " << a.second << " ";
  cout << "$" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char, ll> P;

vector<P> A, B, C;

void input(vector<P> &a) {
  int num;
  char ch;
  while (cin >> ch, ch != '$') {
    cin >> num;
    a.push_back(P(ch, num));
  }
}

bool check(int i) {
  int n = A.size();
  int m = B.size();
  if (i + m > n)
    return 0;
  if (A[i].first != B[0].first || A[i].second < B[0].second)
    return 0;
  if (m == 1)
    return 1;

  i++;
  for (int j = 1; i < m - 1; j++, i++)
    if (A[i].first != B[j].first || A[i].second != B[j].second)
      return 0;

  if (A[i].first != B[m - 1].first || A[i].second < B[m - 1].second)
    return 0;
  return 1;
}

void mergePush(vector<P> &res, P a) {
  int c = a.second;
  char ch = a.first;
  if (c == 0)
    return;
  if (res.size() && res.back().first == ch)
    res.back().second += c;
  else
    res.push_back(P(ch, c));
}

vector<P> solve2() {
  vector<P> res;
  int n = A.size();
  int m = B.size();
  int l = C.size();
  for (int i = 0; i < n; i++) {
    if (!check(i)) {
      mergePush(res, A[i]);
      continue;
    }
    for (int j = 0; j < l; j++)
      mergePush(res, C[j]);
    A[i--].second -= B[0].second;
  }
  return res;
}

vector<P> solve() {
  vector<P> res;
  int n = A.size();
  int m = B.size();
  int l = C.size();
  if (m == 1)
    return solve2();

  for (int i = 0; i < n; i++) {
    if (!check(i)) {
      mergePush(res, A[i]);
      continue;
    }
    mergePush(res, P(A[i].first, A[i].second - B[0].second));
    for (int j = 0; j < l; j++)
      mergePush(res, C[j]);
    i += m - 1;
    A[i--].second -= B.back().second;
  }
  return res;
}

int main() {
  input(A);
  input(B);
  input(C);
  vector<P> ans = solve();
  for (P a : ans)
    cout << a.first << " " << a.second << " ";
  cout << "$" << endl;
  return 0;
}