C++ main.cpp · 99 行 #include <iostream> #include <string> #include <utility> #include <vector> struct Node { int id; Node *prev; Node *next; }; // 머리 노드(번호 0)를 둔 원형 이중 연결 리스트. // 노드는 처음에 한 번만 만들고 다시 늘리지 않으니, 쥐어 둔 Node*가 계속 맞다. class Clips { public: explicit Clips(int n) : nodes_(n + 1) { for (int i = 0; i <= n; i++) { nodes_[i].id = i; nodes_[i].prev = &nodes_[i == 0 ? n : i - 1]; nodes_[i].next = &nodes_[i == n ? 0 : i + 1]; } } Node *at(int x) { return &nodes_[x]; } void move(Node *a, Node *b, Node *c) { order(a, b); if (inside(a, b, c)) return; a->prev->next = b->next; // 떼어 내기 b->next->prev = a->prev; a->prev = c; // c 바로 뒤에 붙이기 b->next = c->next; c->next->prev = b; c->next = a; } void reverse(Node *a, Node *b) { order(a, b); Node *left = a->prev, *right = b->next; for (Node *p = a; p != right;) { Node *after = p->next; // 바꾸기 전에 다음 노드를 쥐어 둔다 std::swap(p->prev, p->next); p = after; } left->next = b; b->prev = left; right->prev = a; a->next = right; } void print(std::ostream &out) { for (Node *p = head()->next; p != head(); p = p->next) out << p->id << (p->next == head() ? '\n' : ' '); } private: std::vector<Node> nodes_; Node *head() { return &nodes_[0]; } // 포인터 a, b를 참조로 받아, a가 앞에 오도록 바꿔 넣는다 void order(Node *&a, Node *&b) { for (Node *p = a; p != head(); p = p->next) if (p == b) return; std::swap(a, b); } static bool inside(const Node *a, const Node *b, const Node *c) { for (const Node *p = a;; p = p->next) { if (p == c) return true; if (p == b) return false; } } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; std::cin >> n >> q; Clips clips(n); std::string cmd; for (int k = 0; k < q; k++) { int a, b, c; std::cin >> cmd; if (cmd == "move") { std::cin >> a >> b >> c; clips.move(clips.at(a), clips.at(b), clips.at(c)); } else if (cmd == "rev") { std::cin >> a >> b; clips.reverse(clips.at(a), clips.at(b)); } else { std::cin >> a; const Node *x = clips.at(a); std::cout << x->prev->id << ' ' << x->next->id << '\n'; } } clips.print(std::cout); return 0; } C main.c · 92 行 #include <stdio.h> #include <string.h> typedef struct Node { int id; struct Node *prev, *next; } Node; /* nodes[0]은 머리 노드(번호 0): 첫 조각의 prev와 마지막 조각의 next가 여기를 가리킨다. nodes[x]는 x번 조각이라, 번호로 바로 그 노드의 주소를 얻는다 */ static Node nodes[10001]; static Node *head = &nodes[0]; /* a에서 next로 걸어 b를 만나면 a가 앞(같아도 앞으로 친다) */ static int comes_first(const Node *a, const Node *b) { for (const Node *p = a; p != head; p = p->next) if (p == b) return 1; return 0; } /* *a가 앞, *b가 뒤가 되게 두 포인터를 바꿔 넣는다 */ static void order(Node **a, Node **b) { if (!comes_first(*a, *b)) { Node *t = *a; *a = *b; *b = t; } } static int inside(const Node *a, const Node *b, const Node *c) { for (const Node *p = a;; p = p->next) { if (p == c) return 1; if (p == b) return 0; } } static void move(Node *a, Node *b, Node *c) { order(&a, &b); if (inside(a, b, c)) return; /* 떼어 내기: 양옆을 서로 잇는다 */ a->prev->next = b->next; b->next->prev = a->prev; /* c 바로 뒤에 붙이기 */ a->prev = c; b->next = c->next; c->next->prev = b; c->next = a; } static void reverse(Node *a, Node *b) { order(&a, &b); Node *left = a->prev, *right = b->next; for (Node *p = a; p != right;) { Node *after = p->next; /* 바꾸기 전에 다음 노드를 쥐어 둔다 */ p->next = p->prev; p->prev = after; p = after; } /* 구간 바깥 두 노드와 구간의 두 끝을 양쪽 방향으로 다시 잇는다 */ left->next = b; b->prev = left; right->prev = a; a->next = right; } int main(void) { int n, q; if (scanf("%d %d", &n, &q) != 2) return 0; for (int i = 0; i <= n; i++) { nodes[i].id = i; nodes[i].prev = &nodes[i == 0 ? n : i - 1]; nodes[i].next = &nodes[i == n ? 0 : i + 1]; } char cmd[8]; for (int k = 0; k < q; k++) { int a, b, c; scanf("%7s", cmd); if (strcmp(cmd, "move") == 0) { scanf("%d %d %d", &a, &b, &c); move(&nodes[a], &nodes[b], &nodes[c]); } else if (strcmp(cmd, "rev") == 0) { scanf("%d %d", &a, &b); reverse(&nodes[a], &nodes[b]); } else { scanf("%d", &a); printf("%d %d\n", nodes[a].prev->id, nodes[a].next->id); } } for (const Node *p = head->next; p != head; p = p->next) printf("%d%c", p->id, p->next == head ? '\n' : ' '); return 0; }