【PAT甲级题解】1097 Deduplication on a Linked List (25分)

Deduplication意为重复数据消除,Deplicated意为复制,是deduplication的过去式,题干大意为给定一个单链表L要求你对每一个结点的值判断,如果这个值的绝对值是第一次出现,那么将它们放在一起,如果这个结点的绝对值已经出现过了,那么按照出现顺序将他们单独存放,最后按照链表的形式输出,先输出第一次出现的主表,然后在输出重复的链表,这里题目有一个坑点,就是有一种3分的情况是没有重复的值得链表,这样的话需要判断再输出,具体方法PAT已经出过好多这种类似的题目了,无非就是对节点地址去重然后根据要求判断最后控制双重key标记的语句输出即可

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<unordered_map>
using namespace std;
const int maxn = 1e5+10;
struct node{
    int data;
    int next;
}list[maxn];
int main() {
    int first, n;
    cin >> first >> n;
    while (n--) {
        int temp;
        cin >> temp;
        cin >> list[temp].data >> list[temp].next;
    }
    vector<int> vt;
    while (first != -1) {
        vt.push_back(first);
        first = list[first].next;
    }
    unordered_map<int, int> mp;
    vector<int> ans[2];
    for (auto it : vt) {
        if (mp[abs(list[it].data)] == 0) {
            ans[0].push_back(it);
            mp[abs(list[it].data)] = 1;
        } else {
            ans[1].push_back(it);
        }
    }
    int key = 0;
    for (auto it : ans[0]) {
        if (!key) {
            printf("%05d %d ", it, list[it].data);
            key = 1;
        } else {
            printf("%05d\n%05d %d ", it, it, list[it].data);
        }
    }
    cout << -1 << endl;
    key = 0;
    for (auto it : ans[1]) {
        if (!key) {
            printf("%05d %d ", it, list[it].data);
            key = 1;
        } else {
            printf("%05d\n%05d %d ", it, it, list[it].data);
        }
    }
    if (key)
        cout << -1 << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苯酸氨酰糖化物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值