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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<string> s;
int x, y;

void gotoend() { y = s[x].size(); }

void gotohome() { y = 0; }

int check() {
  if (s[x].size() == 0 || y == s[x].size())
    return 1;
  else
    return 0;
}

int main() {
  x = 0;
  y = 0;
  string tmp;
  string buf = "";
  while (1) {
    getline(cin, tmp);
    if (tmp == "END_OF_TEXT")
      break;
    s.push_back(tmp);
  }
  char com;
  while (cin >> com) {
    if (com == '-')
      break;
    else if (com == 'a')
      gotohome();
    else if (com == 'e')
      gotoend();
    else if (com == 'p') {
      if (x == 0)
        gotohome();
      else {
        x--;
        gotohome();
      }
    } else if (com == 'n') {
      if (x < s.size() - 1)
        x++;
      gotohome();
    } else if (com == 'f') {
      if (check() == 0)
        y++;
      else if (check() == 1 && x < s.size() - 1) {
        x++;
        gotohome();
      }
    } else if (com == 'b') {
      if (y > 0)
        y--;
      else if (y == 0 && x > 0) {
        x--;
        gotoend();
      }
    } else if (com == 'd') {
      if (check() == 0)
        s[x].erase(y, 1);
      else if (check() == 1 && x < s.size() - 1) {
        s[x] += s[x + 1];
        s.erase(begin(s) + x + 1);
      }
    } else if (com == 'k') {
      if (check() == 1 && x < s.size() - 1) {
        s[x] += s[x + 1];
        s.erase(begin(s) + x + 1);
        buf = "\\n";
      }

      else if (check() == 0) {
        buf = s[x].substr(y);
        s[x].erase(y);
      }
    } else if (com == 'y') {
      if (buf == "\\n") {
        string tmp = s[x].substr(y);
        s.insert(begin(s) + x + 1, tmp);
        s[x].erase(y);
        x++;
        gotohome();
      } else if (buf != "") {
        s[x].insert(y, buf);
        y += buf.size();
      }
    }
  }
  REP(i, s.size()) { cout << s[i] << endl; }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<string> s;
int x, y;

void gotoend() { y = s[x].size(); }

void gotohome() { y = 0; }

int check() {
  if (s[x].size() == 0)
    return 1;
  else if (y == s[x].size() - 1)
    return 2;
  else
    return 0;
}

int main() {
  x = 0;
  y = 0;
  string tmp;
  string buf = "@";
  while (1) {
    cin >> tmp;
    if (tmp == "END_OF_TEXT")
      break;
    s.push_back(tmp);
  }
  char com;
  while (cin >> com) {
    if (com == '-')
      break;
    else if (com == 'a')
      gotohome();
    else if (com == 'e')
      gotoend();
    else if (com == 'p') {
      if (x == 0)
        gotohome();
      else {
        x--;
        gotohome();
      }
    } else if (com == 'n') {
      if (x < s.size() - 1)
        x++;
      gotohome();
    } else if (com == 'f') {
      if (check() == 0)
        y++;
      else if (x < s.size() - 1) {
        x++;
        gotohome();
      }
    } else if (com == 'b') {
      if (y != 0)
        y--;
      else if (y == 0 && x > 0) {
        x--;
        gotoend();
      }
    } else if (com == 'd') {
      if (check() == 0)
        s[x].erase(y, 1);
      else if (check() == 2 && x < s.size() - 1) {
        s[x] += s[x + 1];
        int t = x + 1;
        while (t < s.size() - 1) {
          s[t] = s[t + 1];
          t++;
        }
        s.pop_back();
      }
    } else if (com == 'k') {
      if (check() != 0 && x < s.size() - 1) {
        s[x] += s[x + 1];
        int t = x + 1;
        while (t < s.size() - 1) {
          s[t] = s[t + 1];
          t++;
        }
        s.pop_back();
        buf = "#";
      } else {
        buf = s[x].substr(y);
        s[x].erase(y);
      }
    } else if (com == 'y') {
      if (buf == "@")
        continue;
      else if (buf == "#") {
        int t = s.size() - 1;
        s.push_back("");
        while (t > x) {
          s[t + 1] = s[t];
          t--;
        }
        s[x + 1] = s[x].substr(y);
        s[x].erase(y);
        x++;
        gotohome();
      } else {
        s[x].insert(y, buf);
        y += buf.size();
      }
    }

    /*
    REP(i, s.size())
    {
            cout << s[i] << endl;
    }
    cout << x << " " << y << endl;
    cout << buf;
    cout << endl << endl;
    */
  }
  REP(i, s.size()) { cout << s[i] << endl; }
  return 0;
}