指针初学者求助!!1

P1188 PASTE

wheneveright @ 2021-05-12 20:08:01

这个代码本地和在线 IDE 样例都跑的过去,但是提交全 RE,不知道是哪里出错了 qwq

# include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

struct reader {
    template <typename Type>
    reader & operator >> (Type & ret) {
        int f = 1; ret = 0; char ch = getchar ();
        for (;!isdigit (ch); ch = getchar ()) if (ch == '-') f = -f;
        for (; isdigit (ch); ch = getchar ()) ret = (ret * 10) + ch - '0';
        ret *= f; return *this;
    }
} fin;

int N, Q;
int L, R, id;
struct ListNode {
    int val;
    ListNode * next, * last;
    ListNode () : val (0) {}
} * head, * now, * Lptr, * Rptr, * Qptr, * Empty;

int main () {
    fin >> N >> Q; now = head = new ListNode ();
    for (int i = 1; i <= N; i++) {
        now -> next = new ListNode();
        now -> next -> last = now;
        now = now -> next; now -> val = i;
    }   now -> next = (Empty = new ListNode ()); 
    while (Q--) {
        fin >> L >> R >> id; Lptr = Rptr = Qptr = head;
        for (int i = 1; i <= N; i++) {
            if (i <= L)  Lptr = Lptr -> next;
            if (i <= R)  Rptr = Rptr -> next;
            if (i <= id) Qptr = Qptr -> next;
            if (i > L && i > R && i > id) break;
        }
        Lptr -> last -> next = Rptr -> next;
        Rptr -> next -> last = Lptr -> last;
        Rptr -> next = Qptr -> next;
        Rptr -> next -> last = Rptr;
        Lptr -> last = Qptr;
        Qptr -> next = Lptr;
    }   now = head;
    for (int i = 1; i <= 10; i++) printf ("%d\n", (now = now -> next) -> val);
    return 0;
}

by wheneveright @ 2021-05-12 20:08:31

是用指针实现的链表,暴力修改。


by Macesuted @ 2021-05-12 20:35:09

也许需要释放内存?

我想需要补一个析构函数之类的,不然内存泄漏严重。


by E1_de5truct0r @ 2021-05-12 20:44:58

蒟蒻表示没学过,自闭了(


by 王瑞eggome @ 2021-05-12 21:23:13

指针初学者.txt


by Macesuted @ 2021-05-13 07:58:38

@wheneveright

我刚自己写了一下,发现了问题。

题目的意思实际上是指把指定的一段区间截下来,再从剩下的文本中选取指定行进行插入操作。

即对您的代码中的 id 对应的指针的移动应该在截取区间后再进行。


by wheneveright @ 2021-05-13 08:03:25

@Macesuted 谢谢辣~


|