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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>

using namespace std;

int main() {

  list<int> lnk;

  const int SLEN = 20;
  char com[SLEN];
  int n, key = 0;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s%d", com, &key);

    if (com[0] == 'i') {
      lnk.push_front(key);
    } else if (com[0] == 'd') {

      if (strlen(com) > 6) {
        if (com[6] == 'F') {
          lnk.pop_front();
        } else if (com[6] == 'L') {
          lnk.pop_back();
        }
      } else {
        for (list<int>::iterator it = lnk.begin(); it != lnk.end(); it++) {
          if (*it == key) {
            lnk.erase(it);
            break;
          }
        }
      }
    }
  }

  int i = 0;
  for (list<int>::iterator it = lnk.begin(); it != lnk.end(); it++) {
    if (i++)
      printf(" ");
    printf("%d", *it);
  }
  printf("\\n");

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static const int none = -1;

struct linkItem {
  int key = none;
  int nextIdx = -1;
  int prevIdx = -1;
};

class dblLinkLst {
public:
  dblLinkLst() {}

  ~dblLinkLst() {}

  void insertItem(int insKey) {

    int nextLnk = none;
    for (int i = lLen - 1; i >= 0; i--) {
      if (plst[i].key != none) {
        plst[i].prevIdx = plst.size();

        nextLnk = i;
        break;
      }
    }

    linkItem tmp;
    tmp.key = insKey;
    tmp.nextIdx = nextLnk;

    plst.push_back(tmp);

    lLen = plst.size();
  }

  void deleteItem(int delKey) {

    linkItem lnk;
    int thisIdx = 0;
    lnk.key = none;
    for (int i = lLen - 1; i >= 0; i--) {
      if (plst[i].key != none) {
        lnk = plst[i];
        thisIdx = i;
        break;
      }
    }

    if (lnk.key != none) {
      while (true) {
        if (lnk.key == delKey) {
          if (lnk.prevIdx > none)
            plst[lnk.prevIdx].nextIdx = lnk.nextIdx;
          if (lnk.nextIdx > none)
            plst[lnk.nextIdx].prevIdx = lnk.prevIdx;

          plst[thisIdx].key = none;
          break;
        }

        if (lnk.nextIdx == none)
          break;

        thisIdx = lnk.nextIdx;
        lnk = plst[thisIdx];
      }
    }
    return;
  }

  void deleteFirst() {
    linkItem lnk;
    int thisIdx = 0;

    for (int i = lLen - 1; i >= 0; i--) {
      if (plst[i].key != none) {
        lnk = plst[i];
        thisIdx = i;
        break;
      }
    }

    if (lnk.key != none) {
      if (lnk.nextIdx > none)
        plst[lnk.nextIdx].prevIdx = none;
      plst[thisIdx].key = none;
    }
    return;
  }

  void deleteLast() {
    linkItem lnk;
    int thisIdx = 0;
    for (int i = 0; i < lLen; i++) {
      if (plst[i].key != none) {
        lnk = plst[i];
        thisIdx = i;
        break;
      }
    }

    if (lnk.key != none) {
      if (lnk.prevIdx > none)
        plst[lnk.prevIdx].nextIdx = none;
      plst[thisIdx].key = none;
    }

    return;
  }

  string printKeys() {
    string out;
    linkItem lnk;
    lnk.key = none;
    for (int i = lLen - 1; i > 0; i--) {
      if (plst[i].key != none) {
        lnk = plst[i];
        break;
      }
    }

    if (lnk.key != none) {
      while (lnk.nextIdx > none) {
        out += to_string(lnk.key) + " ";
        lnk = plst[lnk.nextIdx];
      }
      out += to_string(lnk.key);
    }

    return out;
  }

private:
  vector<linkItem> plst;
  int lLen = 0;
  int frontIdx = 0;
};

int main() {

  dblLinkLst dbl;

  const int SLEN = 20;
  char com[SLEN];
  int n, key = 0;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {

    scanf("%s%d", com, &key);

    if (com[0] == 'i') {
      dbl.insertItem(key);
    } else if (com[0] == 'd') {

      if (strlen(com) > 6) {
        if (com[6] == 'F') {
          dbl.deleteFirst();
        } else if (com[6] == 'L') {
          dbl.deleteLast();
        }
      } else {

        dbl.deleteItem(key);
      }
    }
  }

  string ans = dbl.printKeys();
  cout << ans << endl;
  return 0;
}