Problem p00607 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string txt[101] = {}, bf;
int kx, ky, x;

void A() { kx = 0; }
void E() { kx = txt[ky].size(); }
void P() {
  if (ky)
    ky--;
  A();
}
void N() {
  if (x - 1 != ky)
    ky++;
  A();
}
void F() {
  if (kx != txt[ky].size())
    kx++;
  else if (kx == txt[ky].size() && x - 1 != ky)
    N();
}
void B() {
  if (kx)
    kx--;
  else if (!kx && ky) {
    ky--;
    E();
  }
}
void D() {
  if (kx != txt[ky].size()) {
    txt[ky] = txt[ky].substr(0, kx) + txt[ky].substr(kx + 1);
  } else if (kx == txt[ky].size() && ky != x - 1) {
    txt[ky] += txt[ky + 1];
    for (int i = ky + 1; i < x; i++)
      txt[i] = txt[i + 1];
    txt[x] = "";
    x--;
  }
}
void K() {
  if (kx == txt[ky].size() && x - 1 != ky) {
    D();
    bf = "\\n";
  } else if (kx != txt[ky].size()) {
    bf = txt[ky].substr(kx);
    txt[ky] = txt[ky].substr(0, kx);
  }
}
void Y() {
  if (bf == "\\n") {
    // txt[ky+1]=txt[ky].substr(kx)+txt[ky+1];
    // txt[ky]=txt[ky].substr(0,kx);
    // if(ky+1==x)x++;
    string next = txt[ky].substr(kx);
    txt[ky] = txt[ky].substr(0, kx);
    x++;
    for (int i = x - 1; i >= ky + 2; i--)
      txt[i] = txt[i - 1];
    txt[ky + 1] = next;
    N();
  } else if (bf != "") {
    txt[ky] = txt[ky].substr(0, kx) + bf + txt[ky].substr(kx);
    kx += bf.size();
  }
}

int main() {
  string s;
  kx = ky = x = 0;
  bf = "";
  while (getline(cin, s), s != "END_OF_TEXT")
    txt[x++] = s;
  while (cin >> s, s != "-") {
    if (s == "a")
      A();
    if (s == "e")
      E();
    if (s == "p")
      P();
    if (s == "n")
      N();
    if (s == "f")
      F();
    if (s == "b")
      B();
    if (s == "d")
      D();
    if (s == "k")
      K();
    if (s == "y")
      Y();
#if 0
		cerr << "ROW: " << ky << "COL: " << kx << endl;
		for(int i = 0; i < x; i++) cerr << txt[i] << endl;
#endif
  }
  for (int i = 0; i < x; i++)
    cout << txt[i] << endl;
}

Error: TLE

Fixed submission:

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

template <class T> void pv(T a, T b) {
  for (typeof(a) it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}

#define all(v) (v).begin(), (v).end()

vector<string> text;
vector<char> command;

int cursol_row;
int cursol_col;

void com_a() { cursol_col = 0; }

void com_e() { cursol_col = text.at(cursol_row).length() - 1; }

void com_p() {
  if (cursol_row > 0)
    cursol_row--;
  com_a();
}

void com_n() {
  if (cursol_row < (int)text.size() - 1)
    cursol_row++;
  com_a();
}

void com_f() {
  if (text[cursol_row][cursol_col] != '^')
    cursol_col++;
  else if (text[cursol_row][cursol_col] == '^' &&
           cursol_row < (int)text.size() - 1) {
    com_f();
  }
}

void com_b() {
  if (cursol_col > 0)
    cursol_col--;
  else if (cursol_col == 0 && cursol_row > 0) {
    cursol_row--;
    com_e();
  }
}

void show_cursol() {
  cerr << "ROW: " << cursol_row << " COL: " << cursol_col << endl;
}

void com_d() {
  if (text[cursol_row][cursol_col] != '^') {
    string line = text[cursol_row];
    string left = line.substr(0, cursol_col);
    string right = line.substr(cursol_col + 1);
    text[cursol_row] = left + right;
  } else if (text[cursol_row][cursol_col] == '^' &&
             cursol_row < (int)text.size() - 1) {
    string line = text[cursol_row];
    line = line.substr(0, line.length() - 1) + text[cursol_row + 1];
    text[cursol_row] = line;

    vector<string>::iterator it = text.begin();
    advance(it, cursol_row + 1);
    text.erase(it);
  }
}

string buffer;

void com_k() {
  if (text[cursol_row][cursol_col] == '^' &&
      cursol_row < (int)text.size() - 1) {
    com_d();
    buffer = "^";
  } else if (text[cursol_row][cursol_col] != '^') {
    string line = text[cursol_row];
    buffer = line.substr(cursol_col);
    buffer = buffer.substr(0, buffer.length() - 1);
    text[cursol_row] = line.substr(0, cursol_col) + "^";
  }
}

void com_y() {
  if (buffer == "^") {
    string line = text[cursol_row];
    string fs = line.substr(0, cursol_col) + "^";
    string sc = line.substr(cursol_col);
    text[cursol_row] = fs;

    vector<string>::iterator it = text.begin();
    advance(it, cursol_row + 1);
    text.insert(it, sc);
    cursol_row++;
    cursol_col = 0;
  } else if (buffer != "") {
    string line = text[cursol_row];
    string left = line.substr(0, cursol_col);
    string right = line.substr(cursol_col);
    text[cursol_row] = left + buffer + right;
  }
}

int main() {
  buffer = "";

  cursol_row = 0;
  cursol_col = 0;

#if 1
  for (string line; getline(cin, line) && line != "END_OF_TEXT";) {
    text.push_back(line + "^");
  }
  for (char c; cin >> c && c != '-';) {
    command.push_back(c);
  }
#else
  for (string line; getline(cin, line) && line != "-";) {
    text.push_back(line + "^");
  }
  for (char c; cin >> c && c != '-';) {
    switch (c) {
    case 'a':
      com_a();
      break;
    case 'e':
      com_e();
      break;
    case 'p':
      com_p();
      break;
    case 'n':
      com_n();
      break;
    case 'f':
      com_f();
      break;
    case 'b':
      com_b();
      break;
    case 'd':
      com_d();
      break;
    case 'k':
      com_k();
      break;
    case 'y':
      com_y();
      break;
    default:
      break;
    }
    pv(all(text));
    show_cursol();
  }
#endif

#if 0
	pv(all(text));
	pv(all(command));
#endif

#if 1
  for (int i = 0; i < (int)command.size(); i++) {
    char c = command[i];
    switch (c) {
    case 'a':
      com_a();
      break;
    case 'e':
      com_e();
      break;
    case 'p':
      com_p();
      break;
    case 'n':
      com_n();
      break;
    case 'f':
      com_f();
      break;
    case 'b':
      com_b();
      break;
    case 'd':
      com_d();
      break;
    case 'k':
      com_k();
      break;
    case 'y':
      com_y();
      break;
    default:
      break;
    }
  }

  for (int i = 0; i < (int)text.size(); i++) {
    cout << text[i].substr(0, text[i].length() - 1) << endl;
    // cout << text[i] << endl;
  }
#endif

  return 0;
}