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

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

void rem(vector<string> &msg, int idx) {
  vector<string> r;
  for (int i = 0; i < msg.size(); i++)
    if (i != idx)
      r.push_back(msg[i]);
  msg = r;
}

void rem(string &msg, int idx) {
  string r;
  for (int i = 0; i < msg.size(); i++)
    if (i != idx)
      r += msg[i];
  msg = r;
}

vector<string> msg;
string buf = "";
int x, y;

int main() {
  string tmp;
  while (getline(cin, tmp)) {
    if (tmp[tmp.size() - 1] == '\\r')
      tmp = tmp.substr(0, tmp.size() - 1);
    if (tmp == "END_OF_TEXT")
      break;
    msg.push_back(tmp);
  }
  while (cin >> tmp && tmp != "-") {
    if (tmp == "a") {
      x = 0;
    }
    if (tmp == "e") {
      x = msg[y].size();
    }
    if (tmp == "p") {
      x = 0;
      if (y)
        y--;
    }
    if (tmp == "n") {
      x = 0;
      if (y + 1 < msg.size())
        y++;
    }
    if (tmp == "f") {
      if (x != msg[y].size()) {
        x++;
      } else if (x == msg[y].size() && y + 1 < msg.size()) {
        y++;
        x = 0;
      }
    }
    if (tmp == "b") {
      if (x != 0)
        x--;
      else if (x == 0 && y) {
        y--;
        x = msg[y].size();
      }
    }
    if (tmp == "d") {
      if (x < msg[y].size()) {
        rem(msg[y], x);
      } else if (x == msg[y].size() && y + 1 < msg.size()) {
        msg[y] += msg[y + 1];
        rem(msg, y + 1);
      }
    }

    if (tmp == "k") {
      if (x == msg[y].size() &&
          y + 1 < msg.size()) { // ここ日本語怪しいからチェック
        msg[y] += msg[y + 1];
        rem(msg, y + 1);
        buf = "\\n";
      } else if (x != msg[y].size()) {
        buf = msg[y].substr(x);
        msg[y] = msg[y].substr(0, x);
      } // else buf = "\\n";
    }
    if (tmp == "y") {
      if (buf == "") {
      } else if (buf == "\\n") {
        vector<string> a;
        for (int k = 0; k < y; k++)
          a.push_back(msg[k]);
        a.push_back(msg[y].substr(0, x));
        a.push_back(msg[y].substr(x));
        for (int k = y + 1; k < msg.size(); k++)
          a.push_back(msg[k]);
        msg = a;
        y++;
        x = 0;
      } else {
        msg[y] = msg[y].substr(0, x) + buf + msg[y].substr(x);
        x += buf.size();
      }
    }
  }
  for (int i = 0; i < msg.size(); i++)
    cout << msg[i] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a)                                                                \\
  rep(_X, sz(a)) { printf("%d ", a[_X]); }                                     \\
  puts("");
#define DBG2(a)                                                                \\
  rep(_X, sz(a)) {                                                             \\
    rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]);                               \\
    puts("");                                                                  \\
  }
#define bitcount(b) __builtin_popcount(b)

#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
  a.push_back(b);
  return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
  while (b--)
    if (!a.empty())
      a.pop_back();
}
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll b_pow(ll x, ll n) {
  return n ? n == 1 ? x : b_pow(x, n >> 1) * b_pow(x, (n >> 1) + (n & 1)) : 1;
}
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

string buffer;
vector<string> text;
int x, y;

void a() { x = 0; }
void e() { x = text[y].length() - 1; }
void p() {
  int ty = y - 1;
  x = 0;
  if (ty != -1)
    y = ty;
}

void n() {
  int ty = y + 1;
  x = 0;
  if (ty != text.size())
    y = ty;
}

void f() {
  int tx = x + 1;
  if (tx == text[y].length()) {
    int ty = y + 1;
    if (ty != text.size()) {
      y = ty;
      x = 0;
    }
  } else {
    x = tx;
  }
}

void b() {
  int tx = x - 1;
  if (tx == -1) {
    int ty = y - 1;
    if (ty != -1) {
      y = ty;
      x = text[y].length() - 1;
    }
  } else {
    x = tx;
  }
}

bool d() {
  if (text[y][x] == '\\n') {
    int ty = y + 1;
    if (ty != text.size()) {
      text[y].erase(x, 1);
      text[y] += text[ty];
      text[ty] = "";
      return true;
    }
  } else {
    text[y].erase(x, 1);
  }
  return false;
}
void k() {
  if (text[y][x] == '\\n') {
    if (d())
      buffer = "\\n";
  } else {
    buffer = text[y].substr(x);
    buffer.erase(buffer.length() - 1);
    text[y] = text[y].substr(0, x) + "\\n";
  }
}

void Y() {
  if (buffer == "")
    return;

  if (buffer == "\\n") {
    vector<string> tmp;
    string a, b;
    a = text[y].substr(0, x) + "\\n";
    b = text[y].substr(x);
    rep(i, y) tmp.push_back(text[i]);
    tmp.push_back(a), tmp.push_back(b);

    for (int i = y + 1; i < text.size(); i++)
      tmp.push_back(text[i]);
    y = y + 1;

    text = tmp;
  } else {
    text[y].insert(x, buffer);
  }
}

int main() {
  x = 0, y = 0;
  string s;
  while (getline(cin, s)) {
    if (s[s.length() - 1] == '\\r')
      s.erase(s.length() - 1, 1);
    if (s == "END_OF_TEXT")
      break;
    text.push_back(s + "\\n");
  }
  if (text.size() == 0)
    text.push_back("\\n");
  while (cin >> s, s != "-") {
    if (s == "a")
      a();
    if (s == "e")
      e();
    if (s == "p")
      p();
    if (s == "n")
      n();
    if (s == "f")
      f();
    if (s == "b")
      b();
    if (s == "d")
      d();
    if (s == "k")
      k();
    if (s == "y")
      Y();
    text.erase(remove(all(text), ""), text.end());
  }
  rep(i, text.size()) cout << text[i];
}