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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<vector<char>> c(4);
  vector<vector<int>> len(4);
  for (int i = 0; i < 3; i++) {
    while (1) {
      char letter;
      int n;
      cin >> letter;
      if (letter == '$')
        break;
      cin >> n;
      c[i].push_back(letter);
      len[i].push_back(n);
    }
  }

  int asize = c[0].size();
  int bsize = c[1].size();
  int csize = c[2].size();
  for (int i = 0; i < asize - bsize + 1; i++) {
    bool flag = true;
    for (int j = 0; j < bsize; j++) {
      if (c[0][i + j] == c[1][j]) {
        if (j == 0 || j == bsize - 1) {
          if (len[0][i + j] < len[1][j]) {
            flag = false;
            break;
          }
        } else {
          if (len[0][i + j] != len[1][j]) {
            flag = false;
            break;
          }
        }
      } else {
        flag = false;
        break;
      }
    }
    if (flag) {
      int s = i;
      for (int j = 0; j < s; j++) {
        c[3].push_back(c[0][j]);
        len[3].push_back(len[0][j]);
      }
      if (bsize != 1) {
        c[3].push_back(c[0][s]);
        len[3].push_back(len[0][s] - len[1][0]);
      }
      for (int j = 0; j < csize; j++) {
        c[3].push_back(c[2][j]);
        len[3].push_back(len[2][j]);
      }
      c[3].push_back(c[0][s + bsize - 1]);
      len[3].push_back(len[0][s + bsize - 1] - len[1].back());
      for (int j = s + bsize; j < asize; j++) {
        c[3].push_back(c[0][j]);
        len[3].push_back(len[0][j]);
      }
      break;
    }
  }

  if (c[3].empty()) {
    c[3] = c[0];
    len[3] = len[0];
  }
  for (int i = 0; i < (int)c[3].size(); i++) {
    int n = len[3][i];
    char curr = c[3][i];
    for (; i < (int)c[3].size() - 1; i++) {
      if (curr == c[3][i + 1] || len[3][i + 1] == 0) {
        n += len[3][i + 1];
      } else {
        break;
      }
    }
    if (n != 0) {
      cout << curr << " " << n << " ";
    }
  }
  cout << "$" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<vector<char>> c(4);
  vector<vector<int>> len(4);
  for (int i = 0; i < 3; i++) {
    while (1) {
      char letter;
      int n;
      cin >> letter;
      if (letter == '$')
        break;
      cin >> n;
      c[i].push_back(letter);
      len[i].push_back(n);
    }
  }

  int asize = c[0].size();
  int bsize = c[1].size();
  int csize = c[2].size();
  for (int i = 0; i < asize - bsize + 1; i++) {
    bool flag = true;
    for (int j = 0; j < bsize; j++) {
      if (c[0][i + j] == c[1][j]) {
        if (j == 0 || j == bsize - 1) {
          if (len[0][i + j] < len[1][j]) {
            flag = false;
            break;
          }
        } else {
          if (len[0][i + j] != len[1][j]) {
            flag = false;
            break;
          }
        }
      } else {
        flag = false;
        break;
      }
    }
    if (flag) {
      int s = i;
      for (int j = 0; j < s; j++) {
        c[3].push_back(c[0][j]);
        len[3].push_back(len[0][j]);
      }
      c[3].push_back(c[0][s]);
      len[3].push_back(len[0][s] - len[1][0]);

      for (int j = 0; j < csize; j++) {
        c[3].push_back(c[2][j]);
        len[3].push_back(len[2][j]);
      }
      c[3].push_back(c[0][s + bsize - 1]);
      len[3].push_back(len[0][s + bsize - 1] - len[1].back());
      for (int j = s + bsize; j < asize; j++) {
        c[3].push_back(c[0][j]);
        len[3].push_back(len[0][j]);
      }
      break;
    }
  }

  for (int i = 0; i < (int)c[3].size(); i++) {
    int n = len[3][i];
    for (; i < (int)c[3].size() - 1; i++) {
      if (c[3][i] == c[3][i + 1]) {
        n += len[3][i + 1];
      } else {
        i++;
        break;
      }
    }
    i--;
    if (n != 0) {
      cout << c[3][i] << " " << n << " ";
    }
  }
  cout << "$" << endl;

  return 0;
}