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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  list<list<char>> text;
  for (;;) {
    string s;
    getline(cin, s);
    if (s == "END_OF_TEXT")
      break;
    text.push_back(list<char>(s.begin(), s.end()));
  }

  list<list<char>>::iterator it1 = text.begin();
  list<char>::iterator it2 = it1->begin();
  list<char> buffer;
  bool newLine = false;
  for (;;) {
    char c;
    cin >> c;
    if (c == 'a') {
      it2 = it1->begin();
    } else if (c == 'e') {
      it2 = it1->end();
    } else if (c == 'p') {
      if (it1 != text.begin())
        --it1;
      it2 = it1->begin();
    } else if (c == 'n') {
      ++it1;
      if (it1 == text.end())
        --it1;
      it2 = it1->begin();
    } else if (c == 'f') {
      if (it2 != it1->end()) {
        ++it2;
      } else {
        ++it1;
        if (it1 != text.end()) {
          it2 = it1->begin();
        } else {
          --it1;
        }
      }
    } else if (c == 'b') {
      if (it2 != it1->begin()) {
        --it2;
      } else {
        if (it1 != text.begin()) {
          --it1;
          it2 = it1->end();
        }
      }
    } else if (c == 'd') {
      if (it2 != it1->end()) {
        it2 = it1->erase(it2);
      } else {
        list<list<char>>::iterator tmp = it1;
        ++tmp;
        if (tmp != text.end()) {
          it2 = it1->insert(it2, '.');
          it1->splice(it1->end(), *tmp);
          text.erase(tmp);
          it2 = it1->erase(it2);
        }
      }
    } else if (c == 'k') {
      buffer.clear();
      newLine = false;
      if (it2 == it1->end()) {
        list<list<char>>::iterator tmp = it1;
        ++tmp;
        if (tmp != text.end()) {
          it2 = it1->insert(it2, '.');
          it1->splice(it1->end(), *tmp);
          text.erase(tmp);
          it2 = it1->erase(it2);
          newLine = true;
        }
      } else {
        buffer.splice(buffer.end(), *it1, it2, it1->end());
        it2 = it1->end();
      }
    } else if (c == 'y') {
      if (newLine) {
        list<list<char>>::iterator tmp = it1;
        ++it1;
        it1 = text.insert(it1, list<char>());
        it1->splice(it1->end(), *tmp, it2, tmp->end());
        it2 = it1->begin();
      } else if (buffer.size() > 0) {
        list<char> tmp = buffer;
        it1->splice(it2, tmp);
      }
    } else {
      break;
    }
  }

  it1 = text.begin();
  do {
    cout << string(it1->begin(), it1->end()) << endl;
  } while (++it1 != text.end());

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  list<list<char>> text;
  for (;;) {
    string s;
    getline(cin, s);
    if (s == "END_OF_TEXT")
      break;
    text.push_back(list<char>(s.begin(), s.end()));
  }

  list<list<char>>::iterator it1 = text.begin();
  list<char>::iterator it2 = it1->begin();
  list<char> buffer;
  bool newLine = false;
  for (;;) {
    char c;
    cin >> c;
    if (c == 'a') {
      it2 = it1->begin();
    } else if (c == 'e') {
      it2 = it1->end();
    } else if (c == 'p') {
      if (it1 != text.begin())
        --it1;
      it2 = it1->begin();
    } else if (c == 'n') {
      ++it1;
      if (it1 == text.end())
        --it1;
      it2 = it1->begin();
    } else if (c == 'f') {
      if (it2 != it1->end()) {
        ++it2;
      } else {
        ++it1;
        if (it1 == text.end()) {
          --it1;
        } else {
          it2 = it1->begin();
        }
      }
    } else if (c == 'b') {
      if (it2 != it1->begin()) {
        --it2;
      } else {
        if (it1 != text.begin()) {
          --it1;
          it2 = it1->end();
        }
      }
    } else if (c == 'd') {
      if (it2 != it1->end()) {
        it2 = it1->erase(it2);
      } else {
        list<list<char>>::iterator tmp = it1;
        ++tmp;
        if (tmp != text.end()) {
          it2 = it1->insert(it2, '.');
          it1->splice(it1->end(), *tmp);
          text.erase(tmp);
          it2 = it1->erase(it2);
        }
      }
    } else if (c == 'k') {
      buffer.clear();
      newLine = false;
      if (it2 == it1->end()) {
        list<list<char>>::iterator tmp = it1;
        ++tmp;
        if (tmp != text.end()) {
          it2 = it1->insert(it2, '.');
          it1->splice(it1->end(), *tmp);
          text.erase(tmp);
          it2 = it1->erase(it2);
          newLine = true;
        }
      } else {
        buffer.splice(buffer.end(), *it1, it2, it1->end());
        it2 = it1->end();
      }
    } else if (c == 'y') {
      if (newLine) {
        list<list<char>>::iterator tmp = it1;
        ++it1;
        text.insert(it1, list<char>());
        it1->splice(it1->end(), *tmp, it2, it1->end());
        it2 = it1->begin();
      } else if (buffer.size() > 0) {
        list<char> tmp = buffer;
        it1->splice(it2, tmp);
      }
    } else {
      break;
    }
  }

  it1 = text.begin();
  do {
    cout << string(it1->begin(), it1->end()) << endl;
  } while (++it1 != text.end());
}