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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

void convert(string &text, int &cursor, string &buffer, char command);

int main() {
  string text, buffer;
  int cursor = 0;

  for (string line; getline(cin, line) && line != "END_OF_TEXT";
       text += line + '\\n')
    ;
  for (char command; cin >> command && command != '-';
       convert(text, cursor, buffer, command))
    ;

  cout << text;
}

void convert(string &text, int &cursor, string &buffer, char command) {
  switch (command) {
    /*
     a
     J[\\ð»ÝÌsÌæª¶ÉÚ®·éB
     */
  case 'a': {
    if (cursor != 0 && text[cursor - 1] != '\\n')
      while (--cursor)
        if (text[cursor] == '\\n') {
          cursor++;
          break;
        }

    break;
  }
    /*
     e
     J[\\ð»ÝÌsÌsÉÚ®·éB
     */
  case 'e': {
    while (text[cursor] != '\\n')
      cursor++;

    break;
  }
    /*
     p
     ãÉsª êÎAJ[\\ðãÌsÌæª¶ÉÚ®·éB
     ãÉsªÈ¯êÎAJ[\\ðæª¶ÉÚ®·éB
     */
  case 'p': {
    if (cursor != 0)
      while (--cursor)
        if (text[cursor] == '\\n') {
          break;
        }

    if (cursor != 0)
      while (--cursor)
        if (text[cursor] == '\\n') {
          cursor++;
          break;
        }

    break;
  }
    /*
     n
     ºÉsª êÎAJ[\\ðºÌsÌæª¶ÉÚ®·éB
     ºÉsªÈ¯êÎAJ[\\ðæª¶ÉÚ®·éB
     */
  case 'n': {
    int cur = cursor;
    while (text[cur] != '\\n')
      cur++;

    if (cur + 1 == text.size()) {
      if (cursor != 0)
        while (--cursor)
          if (text[cursor] == '\\n') {
            cursor++;
            break;
          }
    } else {
      cursor = cur + 1;
    }

    break;
  }
    /*
     f
     J[\\ªsÉÈ¢êAJ[\\ðPÂEÉÚ®·éB
     J[\\ªsÉ è©ÂºÉsª éêÍAJ[\\ðºÌsÌæª¶ÖÚ®·éB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'f': {
    cursor += cursor + 1 != text.size();

    break;
  }
    /*
     b
     J[\\ªæª¶ÅÈ¢ÈçÎAJ[\\ðPÂ¶ÉÚ®·éB
     J[\\ªæª¶Å è©ÂãÉsª éêÍAJ[\\ðãÌsÌsÖÚ®·éB
     »êÈOÌêÍ½àµÈ¢
     */
  case 'b': {
    cursor -= 0 < cursor;

    break;
  }
    /*
     d
     J[\\ª¶ðw·êÍ»Ì¶ðí·éBí³ê½¶ÌE¤Ì¶ñÍ¶ÉVtg³êéB
     J[\\ªsðwµºÉsª éêÍAºÌsð»ÌÜÜJ[\\ÌÊuÉq°éBÈºÌsÍãÉVtg³êéB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'd': {
    if (cursor + 1 != text.size())
      text.erase(text.begin() + cursor);
    break;
  }
    /*
     k
     J[\\ªsÉ éêAºÉsª êÎAd
     Æ¯¶ìðµobt@ÉPÂÌüsðL^·éB
     J[\\ªsÉÈ¢êAJ[\\ªw·¶ðÜß½E¤·×ÄÌ¶ðØèæè»êðobt@ÉL^·éBJ[\\Í³ÌsÌsðw·æ¤ÉÈéBSÄÌL^É¨¢Äobt@Íã«³êéB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'k': {
    buffer = "";
    if (text[cursor] == '\\n') {
      if (cursor + 1 == text.size())
        break;

      buffer = "\\n";
      text.erase(text.begin() + cursor);
    } else
      while (text[cursor] != '\\n') {
        buffer += text[cursor];
        text.erase(text.begin() + cursor);
      }

    break;
  }
    /*
     y
     obt@ªóÌêÍÈÉàµÈ¢B
     obt@ÌàeªüsÌêAJ[\\ªw·¶Ì¼OÅüsðs¤BJ[\\ÍVµ¢sÌæª¶ÖÚ®·éB
     obt@Ìàeª¶ñÌêAJ[\\ÌÊuÉobt@Ìàeð}ü·éB
     J[\\ÌÊuÍàÆàÆwµÄ¢½¶Ü½ÍsÌêÖÚ®·é
     */
  case 'y': {
    text = text.substr(0, cursor) + buffer + text.substr(cursor);
    cursor += buffer.size();

    break;
  }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

struct Cursor {
  int x, y;
  Cursor() : x(0), y(0) {}
};

void convert(vector<string> &snt, Cursor &cur, string &buffer, char com);

int main() {
  vector<string> snt;
  string buffer;
  Cursor cursor;

  for (string line; getline(cin, line) && line != "END_OF_TEXT";
       snt.push_back(line))
    ;

  for (char com; cin >> com && com != '-'; convert(snt, cursor, buffer, com))
    ;

  rep(i, snt.size()) cout << snt[i] << endl;
}

void convert(vector<string> &snt, Cursor &cur, string &buffer, char com) {
  switch (com) {
    /*
     a
     J[\\ð»ÝÌsÌæª¶ÉÚ®·éB
     */
  case 'a': {
    cur.x = 0;
    break;
  }
    /*
     e
     J[\\ð»ÝÌsÌsÉÚ®·éB
     */
  case 'e': {
    cur.x = snt[cur.y].size();
    break;
  }
    /*
     p
     ãÉsª êÎAJ[\\ðãÌsÌæª¶ÉÚ®·éB
     ãÉsªÈ¯êÎAJ[\\ðæª¶ÉÚ®·éB
     */
  case 'p': {
    cur.y -= cur.y != 0;
    cur.x = 0;
    break;
  }
    /*
     n
     ºÉsª êÎAJ[\\ðºÌsÌæª¶ÉÚ®·éB
     ºÉsªÈ¯êÎAJ[\\ðæª¶ÉÚ®·éB
     */
  case 'n': {
    cur.y += cur.y + 1 != snt.size();
    cur.x = 0;
    break;
  }
    /*
     f
     J[\\ªsÉÈ¢êAJ[\\ðPÂEÉÚ®·éB
     J[\\ªsÉ è©ÂºÉsª éêÍAJ[\\ðºÌsÌæª¶ÖÚ®·éB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'f': {
    if (cur.x != snt[cur.y].size()) {
      cur.x++;
    } else if (cur.y + 1 != snt.size()) {
      cur.y++;
      cur.x = 0;
    }

    break;
  }
    /*
     b
     J[\\ªæª¶ÅÈ¢ÈçÎAJ[\\ðPÂ¶ÉÚ®·éB
     J[\\ªæª¶Å è©ÂãÉsª éêÍAJ[\\ðãÌsÌsÖÚ®·éB
     »êÈOÌêÍ½àµÈ¢
     */
  case 'b': {
    if (cur.x != 0) {
      cur.x--;
    } else if (cur.y != 0) {
      cur.y--;
      cur.x = snt[cur.y].size();
    }

    break;
  }
    /*
     d
     J[\\ª¶ðw·êÍ»Ì¶ðí·éBí³ê½¶ÌE¤Ì¶ñÍ¶ÉVtg³êéB
     J[\\ªsðwµºÉsª éêÍAºÌsð»ÌÜÜJ[\\ÌÊuÉq°éBÈºÌsÍãÉVtg³êéB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'd': {
    if (cur.x != snt[cur.y].size()) {
      string &line = snt[cur.y];

      line = line.substr(0, cur.x) + line.substr(cur.x + 1);
    } else if (cur.y + 1 != snt.size()) {
      snt[cur.y] += snt[cur.y + 1];

      snt.erase(snt.begin() + cur.y + 1);
    }

    break;
  }
    /*
     k
     J[\\ªsÉ éêAºÉsª êÎAd
     Æ¯¶ìðµobt@ÉPÂÌüsðL^·éB
     J[\\ªsÉÈ¢êAJ[\\ªw·¶ðÜß½E¤·×ÄÌ¶ðØèæè»êðobt@ÉL^·éBJ[\\Í³ÌsÌsðw·æ¤ÉÈéBSÄÌL^É¨¢Äobt@Íã«³êéB
     »êÈOÌêÍ½àµÈ¢B
     */
  case 'k': {
    if (cur.x == snt[cur.y].size() && cur.y + 1 != snt.size()) {
      snt[cur.y] += snt[cur.y + 1];

      snt.erase(snt.begin() + cur.y + 1);

      buffer = "\\n";
    } else if (cur.x != snt[cur.y].size()) {
      buffer = snt[cur.y].substr(cur.x);
      snt[cur.y] = snt[cur.y].substr(0, cur.x);
    }

    break;
  }
    /*
     y
     obt@ªóÌêÍÈÉàµÈ¢B
     obt@ÌàeªüsÌêAJ[\\ªw·¶Ì¼OÅüsðs¤BJ[\\ÍVµ¢sÌæª¶ÖÚ®·éB
     obt@Ìàeª¶ñÌêAJ[\\ÌÊuÉobt@Ìàeð}ü·éB
     J[\\ÌÊuÍàÆàÆwµÄ¢½¶Ü½ÍsÌêÖÚ®·é
     */
  case 'y': {
    if (buffer != "") {
      if (buffer == "\\n") {
        string &line = snt[cur.y];

        snt.insert(snt.begin() + cur.y + 1, snt[cur.y].substr(cur.x));
        line = line.substr(0, cur.x);

        cur.y++;
        cur.x = 0;
      } else {
        string &line = snt[cur.y];
        line = line.substr(0, cur.x) + buffer + line.substr(cur.x);

        cur.x += buffer.size();
      }
    }

    break;
  }
  }
}