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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

string s[128];
string buf;
int lines;
void delete_line(int y) {
  for (int i = y; i < lines; ++i)
    s[i] = s[i + 1];

  if (lines > 1)
    --lines;
  s[lines] = "";
}
void add_line(int y) {
  for (int i = lines++; i > y; --i)
    s[i] = s[i - 1];
  s[y] = "";
}
int main() {
  while (true) {
    string t;
    getline(cin, t);
    if (t == "END_OF_TEXT")
      break;
    s[lines++] = t;
  }
  if (lines == 0)
    lines = 1;

  int x, y;
  x = y = 0;
  while (true) {
    char c;
    cin >> c;
    if (c == '-')
      break;

    switch (c) {
    case 'a': {
      x = 0;
      break;
    }
    case 'e': {
      x = s[y].size();
      break;
    }
    case 'p': {
      y = max(0, --y);
      x = 0;
      break;
    }
    case 'n': {
      y = min(lines - 1, ++y);
      x = 0;
      break;
    }
    case 'f': {
      if (x != s[y].size())
        ++x;
      else if (y + 1 < lines)
        x = 0, ++y;
      break;
    }
    case 'b': {
      if (x != 0)
        --x;
      else if (y != 0) {
        --y;
        x = s[y].size();
      }
      break;
    }
    case 'd': {
      if (x != s[y].size())
        s[y].erase(x, 1);
      else if (y + 1 < lines) {
        s[y] += s[y + 1];
        delete_line(y + 1);
      }
      break;
    }
    case 'k': {
      if (x != s[y].size()) {
        buf = s[y].substr(x);
        s[y].erase(x);
      } else if (y + 1 < lines) {
        s[y] += s[y + 1];
        delete_line(y + 1);
        buf = "\\n";
      }
      break;
    }
    case 'y': {
      if (buf == "\\n") {
        add_line(y + 1);
        s[y + 1] = s[y].substr(x);
        s[y].erase(x);
        x = 0, ++y;
      } else {
        s[y].insert(x, buf);
        x += buf.size();
      }
      break;
    }
    }
  }

  for (int i = 0; i < lines; ++i)
    cout << s[i] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

string s[1024];
string buf;
int lines;
void delete_line(int y) {
  for (int i = y; y < lines; ++y)
    s[i] = s[i + 1];
  s[--lines] = "";
  if (lines <= 0)
    lines = 1;
}
void add_line(int y) {
  for (int i = lines++; i > y; ++i)
    s[i] = s[i - 1];
  s[y] = "";
}
int main() {
  while (true) {
    string t;
    getline(cin, t);
    if (t == "END_OF_TEXT")
      break;
    s[lines++] = t;
  }
  if (lines == 0) {
    cout << "" << endl;
    return 0;
  }

  int x, y;
  x = y = 0;
  while (true) {
    char c;
    cin >> c;
    if (c == '-')
      break;

    switch (c) {
    case 'a': {
      x = 0;
      break;
    }
    case 'e': {
      x = s[y].size();
      break;
    }
    case 'p': {
      y = max(0, --y);
      x = 0;
      break;
    }
    case 'n': {
      y = min(lines - 1, ++y);
      x = 0;
      break;
    }
    case 'f': {
      if (x != s[y].size())
        ++x;
      else if (y + 1 != lines)
        x = 0, ++y;
      break;
    }
    case 'b': {
      if (x != 0)
        --x;
      else if (y != 0)
        x = 0, --y;
      break;
    }
    case 'd': {
      if (x != s[y].size()) {
        if (x < s[y].size()) {
          s[y].erase(x, 1);
        } else {
          s[y] += s[y + 1];
          delete_line(y + 1);
        }
      }
      break;
    }
    case 'k': {
      if (x == s[y].size()) {
        s[y] += s[y + 1];
        delete_line(y + 1);
        buf = "\\n";
      } else if (!s[y].empty()) {
        if (x < s[y].size()) {
          buf = s[y].substr(x);
          s[y].erase(x);
        }
        x = s[y].size();
      }
      break;
    }
    case 'y': {
      if (buf == "\\n") {
        add_line(y + 1);
        if (!s[y].empty()) {
          if (x < s[y].size()) {
            s[y + 1] = s[y].substr(x);
            s[y].erase(x);
          }
        }
        x = 0, ++y;
      } else if (!buf.empty())
        s[y] += buf;
      break;
    }
    }

    y = min(lines - 1, max(0, y));
    x = max(0, min(x, (int)s[y].size()));
  }

  for (int i = 0; i < lines; ++i)
    cout << s[i] << endl;
}