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


#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ostream;

#include <cstring>

struct node {
  int data;
  node *prev;
  node *next;
};

struct DLL {
  node *N;

  DLL() : N(new node) {
    N->data = -1;
    N->prev = N;
    N->next = N;
  }

  ~DLL() {}

  node *_insert(int x) {
    node *tmp = new node;
    tmp->data = x;
    tmp->next = N->next;
    tmp->prev = N;

    N->next->prev = tmp;
    N->next = tmp;

    return N;
  }

  node *_delete(int x) {
    for (node *current = N->next; current->data != -1;
         current = current->next) {
      if (current->data == x) {
        current->prev->next = current->next;
        current->next->prev = current->prev;
        break;
      }
    }
    return N;
  }

  node *_deleteFirst() {
    N->next->next->prev = N;
    N->next = N->next->next;
    return N;
  }

  node *_deleteLast() {
    N->prev->prev->next = N;
    N->prev = N->prev->prev;
    return N;
  }

  void print() {
    bool flag = false;
    for (node *current = N->next; current->data != -1;
         current = current->next) {
      if (flag) {
        cout << " ";
      } else {
        flag = true;
      }

      cout << current->data;
    }
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;

  DLL *list = new DLL;

  for (int i = 0; i < n; ++i) {
    char op[20];
    int x;

    cin >> op;

    if (op[0] == 'i') {
      cin >> x;
      list->_insert(x);
    } else if (op[0] == 'd') {
      if (strlen(op) > 6) {
        if (op[6] == 'F')
          list->_deleteFirst();
        else if (op[6] == 'L')
          list->_deleteLast();
      } else {
        cin >> x;
        list->_delete(x);
      }
    }
  }

  list->print();
}


Error: TLE

Fixed submission:


#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::ostream;

#include <string>
using std::string;

#include <map>
using std::map;

struct node {
  int data;
  node *prev;
  node *next;
};

ostream &operator<<(ostream &, node *);

struct DLL {
  node *N;

  DLL() : N(new node) {
    N->data = -1;
    N->prev = N;
    N->next = N;
  }

  ~DLL() {}

  node *_insert(int x) {
    node *tmp = new node;
    tmp->data = x;
    tmp->next = N->next;
    tmp->prev = N;

    N->next->prev = tmp;
    N->next = tmp;

    return N;
  }

  node *_delete(int x) {
    for (node *current = N->next; current->data != -1;
         current = current->next) {
      if (current->data == x) {
        current->prev->next = current->next;
        current->next->prev = current->prev;
        break;
      }
    }
    return N;
  }

  node *_deleteFirst() {
    N->next->next->prev = N;
    N->next = N->next->next;
    return N;
  }

  node *_deleteLast() {
    N->prev->prev->next = N;
    N->prev = N->prev->prev;
    return N;
  }

  void print() {
    bool flag = false;
    for (node *current = N->next; current->data != -1;
         current = current->next) {
      if (flag) {
        cout << " ";
      } else {
        flag = true;
      }

      cout << current;
    }
    cout << endl;
  }
};

ostream &operator<<(ostream &out, node *N) { return out << N->data; }

int main() {
  int n;
  cin >> n;

  DLL *list = new DLL;

  map<string, int> op_dict{
      {"insert", 1}, {"delete", 2}, {"deleteFirst", 3}, {"deleteLast", 4}};

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

    cin >> operation;

    switch (op_dict[operation]) {
    case 1:
      cin >> x;
      list->_insert(x);
      break;
    case 2:
      cin >> x;
      list->_delete(x);
      break;
    case 3:
      list->_deleteFirst();
      break;
    case 4:
      list->_deleteLast();
      break;
    default:
      break;
    }
  }

  list->print();
}