• pat L2-002 链表去重


    题目大意:对一个链表进行去重操作,如果一个数字的绝对值已经出现过了,就取出这个节点,组成一个新的链表。

    分析:直接模拟就好了。刚开始的时候对五位数是用string进行处理的,然后tle,然后还有dfs1e5次导致re。好迷啊这个题,写了一个多小时。有毒!!!

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+7;
    struct node{
        int data;
        int next;
        int pos;
    }a[maxn];
    map<int,bool> vis;
    node rem[maxn],del[maxn];
    int rem_num,del_num;
    void dfs(int pos) {
        rem_num = del_num = 0;
        while (pos != -1) {
            if (vis[abs(a[pos].data)])
                del[del_num].data = a[pos].data, del[del_num].next = -1, del[del_num].pos = pos, del_num++;
            else
                rem[rem_num].data = a[pos].data, rem[rem_num].next = -1, rem[rem_num].pos = pos, rem_num++;
            vis[abs(a[pos].data)] = true;
            pos = a[pos].next;
        }
    }
    int main() {
        int n, start, now, nex, x;
        cin >> start >> n;
        for (int i = 0; i < n; i++) {
            cin >> now >> x >> nex;
            a[now].next = nex;
            a[now].data = x;
        }
        dfs(start);
        if (rem_num) {
            for (int i = 0; i < rem_num - 1; i++)
                printf("%05d %d %05d
    ", rem[i].pos, rem[i].data, rem[i + 1].pos);
            printf("%05d %d %d
    ", rem[rem_num-1].pos, rem[rem_num-1].data, -1);
        }
        if (del_num) {
            for (int i = 0; i < del_num - 1; i++)
                printf("%05d %d %05d
    ", del[i].pos, del[i].data, del[i + 1].pos);
            printf("%05d %d %d
    ", del[del_num-1].pos, del[del_num-1].data, -1);
        }
        return 0;
    }
  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/SwiftAC/p/12180870.html
Copyright © 2020-2023  润新知