alexjercan/bugnet huggingface dataset:
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  list<int> List; //?????????
  int CommandNum; //????????°
  string Command;
  int Number;

  List.clear();

  cin >> CommandNum;

  for (int i = 0; i < CommandNum; i++) {
    cin >> Command;
    if (Command == "insert") { //??????????????\\
            cin >> Number;
      List.push_front(Number);
    } else if (Command == "delete") { //????????????????????????
      cin >> Number;
      list<int>::iterator it = find(List.begin(), List.end(), Number);
      if (it != List.end())
        List.erase(it);
    } else if (Command == "deleteFirst") { //????????????
      List.pop_front();
    } else { //????°???????
      List.pop_back();
    }
  }

  for (list<int>::iterator it = List.begin(); it != List.end(); it++) {
    if (it != List.begin())
      cout << " ";
    cout << *it;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> List; //?????????
  int CommandNum;   //????????°
  string Command;
  int Number;

  List.clear();

  cin >> CommandNum;

  for (int i = 0; i < CommandNum; i++) {
    cin >> Command;
    if (Command == "insert") { //??????????????\\
            cin >> Number;
      List.insert(List.begin(), Number);
      continue;
    } else if (Command == "delete") { //????????????????????????
      cin >> Number;
      vector<int>::iterator it = List.begin();
      for (int i = 0; i < List.size(); i++) {
        if (Number == List[i]) {
          List.erase(it);
          break;
        }
        it++;
      }
      continue;
    } else if (Command == "deleteFirst") { //????????????
      List.erase(List.begin());
      continue;
    } else { //????°???????
      List.erase(List.end() - 1);
      continue;
    }
  }

  for (int i = 0; i < List.size(); i++) {
    if (i > 0)
      cout << " ";
    cout << List[i];
  }
  cout << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  list<int> l;
  string s;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      list<int>::iterator it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

  for (list<int>::iterator it = l.begin(); it != l.end(); ++it) {
    if (it != l.begin())
      cout << ' ';
    cout << *it;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  list<int> l;
  string s;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      list<int>::iterator it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

  for (list<int>::iterator it = l.begin(); it != l.end(); ++it) {
    if (it != l.begin())
      cout << ' ';
    cout << *it;
  }
  cout << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
struct node {
  int key;
  node *pre, *next;
};
node *nil;
void init() {
  nil = (node *)malloc(sizeof(node));
  nil->pre = nil;
  nil->next = nil;
}
void insert(int k) {
  node *x = (node *)malloc(sizeof(node));
  x->key = k;
  x->next = nil->next;
  nil->next->pre = x;
  nil->next = x;
  x->pre = nil;
}

node *searchList(int k) {
  node *p = nil->next;
  while (p != nil && p->key != k) {
    p = p->next;
  }
  return p;
}
void deleteNode(node *t) {
  if (t == nil)
    return;
  t->pre->next = t->next;
  t->next->pre = t->pre;
  free(t);
}
void deleteKey(int key) { deleteNode(searchList(key)); }
void deleteFirst() { deleteNode(nil->next); }
void deleteLast() { deleteNode(nil->pre); }
void printList() {
  node *p = nil->next;
  int sf = 0;
  while (p != nil) {
    if (sf++ > 0)
      printf(" ");
    printf("%d", p->key);
    p = p->next;
  }
  printf("\\n");
}
int main() {
  init();
  int n;
  scanf("%d", &n);
  char s[20];
  int key;
  for (int i = 0; i < n; i++) {
    scanf("%s%d", s, &key);
    if (s[0] == 'd') {
      if (strlen(s) > 6) {
        if (s[6] == 'F') {
          deleteFirst();
        } else if (s[6] == 'L') {
          deleteLast();
        }
      } else {
        deleteKey(key);
      }
    } else if (s[0] == 'i') {
      insert(key);
    }
  }
  printList();
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
struct node {
  int key;
  node *pre, *next;
};
node *nil;
void init() {
  nil = (node *)malloc(sizeof(node));
  nil->pre = nil;
  nil->next = nil;
}
void insert(int k) {
  node *x = (node *)malloc(sizeof(node));
  x->key = k;
  x->next = nil->next;
  nil->next->pre = x;
  nil->next = x;
  x->pre = nil;
}

node *searchList(int k) {
  node *p = nil->next;
  while (p != nil && p->key != k) {
    p = p->next;
  }
  return p;
}
void deleteNode(int k) {
  node *p = searchList(k);
  p->pre->next = p->next;
  p->next->pre = p->pre;
  free(p);
}
void deleteFirst() {
  node *x = nil->next;
  nil->next = x->next;
  x->next->pre = nil;
  free(x);
}
void deleteLast() {
  node *x = nil->pre;
  nil->pre = x->pre;
  x->pre->next = nil;
  free(x);
}
void printList() {
  node *p = nil->next;
  int sf = 0;
  while (p != nil) {
    if (sf++ > 0)
      printf(" ");
    printf("%d", p->key);
    p = p->next;
  }
  printf("\\n");
}
int main() {
  init();
  int n;
  scanf("%d", &n);
  char s[20];
  int key;
  for (int i = 0; i < n; i++) {
    scanf("%s%d", s, &key);
    if (s[0] == 'd') {
      if (strlen(s) > 6) {
        if (s[6] == 'F') {
          deleteFirst();
        } else if (s[6] == 'L') {
          deleteLast();
        }
      } else {
        deleteNode(key);
      }
    } else if (s[0] == 'i') {
      insert(key);
    }
  }
  printList();
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  deque<int> a;
  int x;
  string mission;
  for (int i = 0; i < n; i++) {
    cin >> mission;
    if (mission[0] == 'i') {
      cin >> x;
      a.push_front(x);
    } else if (mission[6] == 'F') {
      a.pop_front();
    } else if (mission[6] == 'L') {
      a.pop_back();
    } else {
      cin >> x;
      for (int s = 0; s < a.size(); s++) {
        if (a[s] == x) {
          a.erase(a.begin() + s);
          break;
        }
      }
    }
  }
  for (int i = 0; i < a.size(); i++) {
    cout << a[i];
    if (i != a.size() - 1)
      cout << " ";
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
#define _CRT_SECURE_NO_WARNINGS
int main() {
  int n;
  cin >> n;
  vector<int> a;
  vector<int>::iterator aho;
  for (int i = 0; i < n; i++) {
    string mission;
    int x;
    cin >> mission;
    if (mission == "insert" || mission == "delete")
      cin >> x;
    if (mission == "insert") {
      a.insert(a.begin(), x);
    } else if (mission == "delete") {
      for (int s = 0; s < a.size(); s++) {
        if (a[s] == x) {
          a.erase(a.begin() + s);
          break;
        }
      }
    } else if (mission == "deleteFirst") {
      a.erase(a.begin());
    } else if (mission == "deleteLast") {
      a.pop_back();
    }
  }
  for (int i = 0; i < a.size(); i++) {
    cout << a[i];
    if (i != a.size() - 1)
      cout << " ";
  }
  cout << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

class DLList {
private:
  //    node *z;
public:
  node *z;
  DLList() {
    z = new node;
    z->key = -1;
    z->next = z;
    z->prev = z;
  }

  ~DLList() {
    node *tmp = z->next;
    node *tmpnext;

    while (tmp != z) {
      tmpnext = tmp->next;
      delete tmp;
      tmp = tmpnext;
    }
    delete z;
  }

  void insert(int v) {
    node *tmp;
    tmp = new node;
    tmp->prev = z;
    tmp->next = z->next;
    tmp->key = v;
    z->next->prev = tmp;
    z->next = tmp;
  }

  void deleteKey(int v) {
    node *tmp;
    tmp = z->next;

    while (tmp != z) {
      if (tmp->key == v) {
        tmp->prev->next = tmp->next;
        tmp->next->prev = tmp->prev;
        delete tmp;
        break;
      }
      tmp = tmp->next;
    }
  }

  void deleteFirst() {
    if (z->next != z) {
      node *tmp;
      tmp = z->next;
      tmp->next->prev = z;
      z->next = z->next->next;
      delete tmp;
    }
  }

  void deleteLast() {
    if (z->prev != z) {
      node *tmp;
      tmp = z->prev;
      tmp->prev->next = z;
      z->prev = z->prev->prev;
      delete tmp;
    }
  }

  void show() {
    node *tmp;
    tmp = z->next;

    if (tmp != z) {
      cout << tmp->key;
      tmp = tmp->next;
    }

    while (tmp != z) {
      cout << " " << tmp->key;
      tmp = tmp->next;
    }

    cout << endl;
  }
};

int main(void) {
  DLList li;
  int n;
  string op;
  int v;
  int i;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> op;

    if (op == "insert") {
      cin >> v;
      li.insert(v);
    }

    if (op == "delete") {
      cin >> v;
      li.deleteKey(v);
    }

    if (op == "deleteFirst") {
      li.deleteFirst();
    }

    if (op == "deleteLast") {
      li.deleteLast();
    }
  }

  li.show();
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  long arr[100002];
  int first = -1;
  int n;
  string command;
  long num;

  int i, j;

  arr[0] = -1;
  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> num;
      first++;
      arr[first] = num;
    }

    if (command == "delete") {
      cin >> num;
      for (j = first; 0 <= j; j--) {
        if (arr[j] == num)
          break;
      }
      if (j != -1) {
        for (; j < first; j++)
          arr[j] = arr[j + 1];
        first--;
      }
    }

    if (command == "deleteFirst") {
      first--;
    }

    if (command == "deleteLast") {
      for (j = 0; j < first; j++)
        arr[j] = arr[j + 1];
      first--;
    }
  }

  for (i = first; 0 <= i; i--) {
    cout << arr[i];
    if (i)
      cout << " ";
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <utility>

using namespace std;

list<int> l;

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int orders;
  cin >> orders;
  for (int i = 0; i < orders; ++i) {
    string order;
    cin >> order;
    if (order == "insert") {
      int x;
      cin >> x;
      l.emplace_front(x);
    } else if (order == "delete") {
      int x;
      cin >> x;
      for (auto ite = l.begin(); ite != l.end(); ++ite) {
        if (*ite == x) {
          l.erase(ite);
          break;
        }
      }
    } else if (order == "deleteFirst")
      l.pop_front();
    else
      l.pop_back();
  }
  auto ite = l.begin();
  cout << *ite;
  ++ite;
  while (ite != l.end()) {
    cout << " " << *ite;
    ++ite;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <utility>

using namespace std;

list<int> l;

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int orders;
  cin >> orders;
  for (int i = 0; i < orders; ++i) {
    string order;
    cin >> order;
    if (order == "insert") {
      int x;
      cin >> x;
      l.emplace_front(x);
    } else if (order == "delete") {
      int x;
      cin >> x;
      for (auto ite = l.begin(); ite != l.end(); ++ite) {
        if (*ite == x) {
          l.erase(ite);
          break;
        }
      }
    } else if (order == "deleteFirst")
      l.pop_front();
    else
      l.pop_back();
  }
  while (l.size() != 1 && !l.empty()) {
    cout << l.front() << " ";
    l.pop_front();
  }
  cout << l.front() << endl;
  l.pop_front();
}
Problem p02265 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string str;
  list<int> data;
  list<int>::iterator it, l;
  int n, x;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      data.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      for (it = data.begin(), l = data.end(); it != l; ++it) {
        if (*it == x) {
          data.erase(it);
          break;
        }
      }
    } else if (str == "deleteFirst")
      data.pop_front();
    else
      data.pop_back();
  }
  for (it = data.begin(), l = --data.end(); it != l; ++it)
    cout << *it << " ";
  cout << *it << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  int n;
  string command;
  int x;
  list<int> intlist;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> x;
      intlist.push_back(x);
    } else if (command == "delete") {
      cin >> x;
      list<int>::iterator it = intlist.end(), fin = intlist.begin();
      it--;
      fin--;
      for (; (it != fin) && (*it != x); it--) {
      }
      intlist.erase(it);
    } else if (command == "deleteFirst") {
      intlist.pop_back();
    } else if (command == "deleteLast") {
      intlist.pop_front();
    }
  }
  for (list<int>::reverse_iterator rit = intlist.rbegin();
       rit != intlist.rend(); rit++) {
    if (rit != intlist.rbegin())
      cout << " ";
    cout << *rit;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>

struct Node {
  int key;
  Node *next, *prev;
};

Node *nil;
void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void printList() {
  Node *cur = nil->next;
  int isf = 0;
  while (1) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

Node *listSearch(int key) {
  Node *cur = nil->next;
  while (cur != nil && cur->key != key)
    cur = cur->next;
  return cur;
}

void deleteNode(Node *t) {
  if (t == nil)
    return;
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

int main() {
  int key, n, i;
  char com[20];
  scanf("%d", &n);
  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i')
      insert(key);
    else if (strlen(com) == 6)
      deleteKey(key);
    else if (com[6] == 'F')
      deleteFirst();
    else
      deleteLast();
  }

  printList();

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

struct Node {
  int key;
  Node *next, *prev;
};

Node *nil;
void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void printList() {
  Node *cur = nil->next;
  int isf = 0;
  while (1) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

Node *listSearch(int key) {
  Node *cur = nil->next;
  while (cur->key != key)
    cur = cur->next;
  return cur;
}

void deleteNode(Node *t) {
  if (t == nil)
    return;
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

int main() {
  int key, n, i;
  char com[20];
  scanf("%d", &n);
  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i')
      insert(key);
    else if (strlen(com) == 6)
      deleteKey(key);
    else if (com[6] == 'F')
      deleteFirst();
    else
      deleteLast();
  }

  printList();

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>
#include <string>

using namespace std;

int main(int argc, char const *argv[]) {
  std::list<int> lst;
  //??\\???
  int count;
  cin >> count;
  string cmd;
  int num;
  for (int i = 0; i < count; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> num;
      lst.push_front(num);
    } else if (cmd == "delete") {
      cin >> num;
      for (auto itr = lst.begin(); itr != lst.end(); ++itr) {
        if (*itr == num) {
          lst.erase(itr);
          break;
        }
      }
    } else if (cmd == "deleteFirst") {
      lst.pop_front();
    } else if (cmd == "deleteLast") {
      lst.pop_back();
    }
  }
  //??????
  bool isfirst = true;
  for (auto itr = lst.begin(); itr != lst.end(); ++itr) {
    if (isfirst) {
      isfirst = false;
    } else {
      cout << ' ';
    }
    cout << *itr;
  }

  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <string>

using namespace std;

int main(int argc, char const *argv[]) {
  std::list<int> lst;
  //??\\???
  int count;
  cin >> count;
  for (int i = 0; i < count; i++) {
    string cmd;
    cin >> cmd;
    if (cmd == "insert") {
      int num;
      cin >> num;
      lst.push_front(num);
    } else if (cmd == "delete") {
      int num;
      cin >> num;
      for (auto itr = lst.begin(); itr != lst.end(); ++itr) {
        if (*itr == num) {
          lst.erase(itr);
          break;
        }
      }
    } else if (cmd == "deleteFirst") {
      lst.pop_front();
    } else if (cmd == "deleteLast") {
      lst.pop_back();
    }
  }
  //??????
  bool isfirst = true;
  for (auto itr = lst.begin(); itr != lst.end(); ++itr) {
    if (isfirst) {
      isfirst = false;
    } else {
      cout << ' ';
    }
    cout << *itr;
  }

  cout << endl;

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  list<int> l;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s;
    if (s == "insert" || s == "delete")
      cin >> x;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      l.pop_back();
      continue;
    }
    if (s == "insert") {
      l.push_front(x);
      continue;
    }
    if (s == "delete") {
      auto it = find(l.begin(), l.end(), x);
      if (it == l.end())
        continue;
      l.erase(it);
      continue;
    }
  }
  bool f = 0;
  for (auto it = l.begin(); it != l.end(); ++it) {
    if (f)
      cout << " ";
    cout << *it;
    f = 1;
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
signed main() {
  cin.tie(0);
  int n;
  cin >> n;
  list<int> l;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s;
    if (s == "insert" || s == "delete")
      cin >> x;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      l.pop_back();
      continue;
    }
    if (s == "insert") {
      l.push_front(x);
      continue;
    }
    if (s == "delete") {
      auto it = find(l.begin(), l.end(), x);
      if (it == l.end())
        continue;
      l.erase(it);
      continue;
    }
  }
  bool f = 0;
  for (auto it = l.begin(); it != l.end(); ++it) {
    if (f)
      cout << " ";
    cout << *it;
    f = 1;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node {
  int key;
  Node *pre;
  Node *next;
};
Node *nil;
void init() {
  nil = new Node;
  nil->pre = nil;
  nil->next = nil;
}
void insert(int x) {
  Node *add = new Node;
  add->key = x;
  add->next = nil->next;
  nil->next->pre = add;
  nil->next = add;
  add->pre = nil;
  add->next;
}
void deleteNode(Node *d) {
  if (d == nil)
    return;
  Node *p = d->pre;
  Node *n = d->next;
  p->next = n;
  n->pre = p;
  free(d);
}
void deleteOne(int x) {
  Node *i = nil->next;
  while (i != nil && i->key != x)
    i = i->next;
  deleteNode(i);
}
void deleteFirst() { deleteNode(nil->next); }
void deleteLast() { deleteNode(nil->pre); }
int main() {
  init();
  int len;
  cin >> len;
  for (int i = 0; i < len; i++) {
    char op[20];
    int x;
    scanf("%s", op);
    if (op[0] == 'i') {
      scanf("%d", &x);
      insert(x);
    } else if (op[0] == 'd') {
      if (strlen(op) <= 6) {
        scanf("%d", &x);
        deleteOne(x);
      } else if (op[6] == 'F')
        deleteFirst();
      else
        deleteLast();
    }
  }
  Node *item = nil->next;
  while (nil->pre != item) {
    printf("%d ", item->key);
    item = item->next;
  }
  printf("%d\\n", item->key);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node {
  int key;
  Node *pre;
  Node *next;
};
Node *nil;
void init() {
  nil = new Node;
  nil->pre = nil;
  nil->next = nil;
}
void insert(int x) {
  Node *add = new Node;
  add->key = x;
  add->next = nil->next;
  nil->next->pre = add;
  nil->next = add;
  add->pre = nil;
  add->next;
}
void deleteNode(Node *d) {
  if (d == nil)
    return;
  Node *p = d->pre;
  Node *n = d->next;
  p->next = n;
  n->pre = p;
  free(d);
}
void deleteOne(int x) {
  Node *i = nil->next;
  while (i->key != x)
    i = i->next;
  deleteNode(i);
}
void deleteFirst() { deleteNode(nil->next); }
void deleteLast() { deleteNode(nil->pre); }
int main() {
  init();
  int len;
  cin >> len;
  for (int i = 0; i < len; i++) {
    char op[20];
    int x;
    scanf("%s", op);
    if (op[0] == 'i') {
      scanf("%d", &x);
      insert(x);
    } else if (op[0] == 'd') {
      if (strlen(op) <= 6) {
        scanf("%d", &x);
        deleteOne(x);
      } else if (op[6] == 'F')
        deleteFirst();
      else
        deleteLast();
    }
  }
  Node *item = nil->next;
  while (nil->pre != item) {
    printf("%d ", item->key);
    item = item->next;
  }
  printf("%d\\n", item->key);
  return 0;
}