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

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

int main() {
  vector<string> line;
  string s, buff;
  int y = 0, x = 0;

  while (getline(cin, s)) {
    if (s == "END_OF_TEXT")
      break;
    line.push_back(s);
  }
  while (getline(cin, s)) {
    if (s == "-")
      break;

    if (s[0] == 'a') {
      x = 0;
    } else if (s[0] == 'e') {
      x = line[y].size();
    } else if (s[0] == 'p') {
      if (y == 0) {
        x = 0;
      } else {
        y--;
        x = 0;
      }
    } else if (s[0] == 'n') {
      if (y == line.size() - 1) {
        x = 0;
      } else {
        y++;
        x = 0;
      }
    } else if (s[0] == 'f') {
      if (x != line[y].size()) {
        x++;
      } else if (y != line.size() - 1) {
        y++;
        x = 0;
      }
    } else if (s[0] == 'b') {
      if (x != 0) {
        x--;
      } else if (y != 0) {
        y--;
        x = line[y].size();
      }
    } else if (s[0] == 'd') {
      if (x != line[y].size()) {
        string str;
        for (int i = 0; i < line[y].size(); i++) {
          if (i == x)
            continue;
          str.push_back(line[y][i]);
        }
        line[y] = str;
      } else if (y != line.size() - 1) {
        line[y] += line[y + 1];
        for (int i = y + 2; i < line.size(); i++) {
          line[i - 1] = line[i];
        }
        line.pop_back();
      }
    } else if (s[0] == 'k') {
      if (x == line[y].size()) {
        if (y != line.size() - 1) {
          buff = ";";
          line[y] += line[y + 1];
          for (int i = y + 2; i < line.size(); i++) {
            line[i - 1] = line[i];
          }
          line.pop_back();
        }
      } else {
        string str;
        for (int i = x; i < line[y].size(); i++) {
          str.push_back(line[y][i]);
        }
        buff = str;
        str.clear();
        for (int i = 0; i < x; i++) {
          str.push_back(line[y][i]);
        }
        line[y] = str;
      }
    } else if (s[0] == 'y') {
      if (!buff.empty()) {
        if (buff == ";") {
          string str;
          for (int i = x; i < line[y].size(); i++) {
            str.push_back(line[y][i]);
          }
          line.push_back(line[line.size() - 1]);
          for (int i = line.size() - 2; i >= y + 2; i--) {
            line[i] = line[i - 1];
          }
          line[y + 1] = str;
          str.clear();
          for (int i = 0; i < x; i++) {
            str.push_back(line[y][i]);
          }
          line[y] = str;
          y++;
          x = 0;
        } else {
          string str;
          for (int i = 0; i < x; i++) {
            str.push_back(line[y][i]);
          }
          str += buff;
          for (int i = x; i < line[y].size(); i++) {
            str.push_back(line[y][i]);
          }
          line[y] = str;
          x += buff.size();
        }
      }
    }
  }
  for (int y_ = 0; y_ < line.size(); y_++) {
    for (int x_ = 0; x_ < line[y_].size(); x_++) {
      cout << line[y_][x_];
    }
    cout << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  string s;
  string t[111];
  string buf = "";
  int row = 0;
  int col = 0;
  int line = 0;
  for (int i = 0; i < 111; i++)
    t[i] = "";

  for (int i = 0; getline(cin, s); i++) {
    if (s == "END_OF_TEXT") {
      break;
    }
    if (i < 110)
      t[i] = s;
    line = i + 1;
  }
  for (; getline(cin, s);) {
    if (s == "-") {
      break;
    }

    if (s == "a") {
      col = 0;
    } else if (s == "e") {
      if (row < 110)
        col = t[row].size();
    } else if (s == "p") {
      col = 0;
      if (row > 0) {
        row--;
      }
    } else if (s == "n") {
      col = 0;
      if (row < line)
        row++;
    } else if (s == "f") {
      if (row < 110 && col != (int)t[row].size()) {
        col++;
      } else {
        if (row + 1 != line) {
          col = 0;
          row++;
        }
      }
    } else if (s == "b") {
      if (col != 0) {
        col--;
      } else {
        if (row != 0 && row < 110) {
          col = t[--row].size();
        }
      }
    } else if (s == "d") {
      if (row < 110 && col != (int)t[row].size()) {
        t[row].erase(col, 1);
      } else {
        if (row < 110 && row + 1 != line) {
          t[row] += t[row + 1];
          t[row] = "";
          line--;
        }
      }
    } else if (s == "k") {
      if (row < 110 && col == (int)t[row].size()) {
        if (row + 1 != line) {
          t[row] += t[row + 1];
          t[row] = "";
          line--;
          buf = "\\n";
        }
      } else if (row < 110) {
        buf = t[row].substr(col);
        t[row].erase(col);
      }
    } else if (s == "y") {
      if (buf != "") {
        if (row < 110 && buf == "\\n") {
          for (int i = line - 1; i > row; i--) {
            if (line + 1 < 110 && line >= 0)
              t[line + 1] = t[line];
          }
          t[row].substr(col);
          // t[row+1] = ;
          // t[row+1] =
          // if( col != t[row].size() ) t[row];//.substr( col );
          // if(col != t[row].size() ) t[row].erase( 0 );
          col = 0;
          row++;
          line++;
        } else if (row < 110) {
          t[row].insert(col, buf);
          col += buf.size();
        }
      }
    }
  }
  for (int i = 0; i < line; i++) {
    cout << t[i] << endl;
  }
}