PAT A1097 Deduplication on a Linked List (25)

原题:


1097. Deduplication on a Linked List (25)

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (<= 105 10 5 ) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 104 10 4 , and Next is the position of the next node.

Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

Sample Output:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

题解:


题目大意

That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept
上述意思就是, 对于每一个k,仅保留首次出现的k,以后出现的都进行remove
需求:输出去重后的list,并且保留按顺序被去重的node

输入

首行:首节点的地址 总的节点数N
    -- 地址格式:有效地址为5为非负数字,无效NULL为-1
    -- N <= 10^5
N行:地址 key值 next地址 
    -- key值 <= 10^4

输出

输出去重后的list,然后接着输出去重后的list
每个node占据一行,与输入格式相同

评测结果

PAT1097

代码实现

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

const uint32_t max_key = 10001;
const uint32_t max_addr = 100001;
struct Node
{
    int32_t addr;
    int32_t key;
    int32_t next;
}node_pack[max_addr];


uint32_t _abs(int key)
{
    return key >= 0 ? key : -key;
}

int main ()
{
    int32_t head = -1, N = 0;
    scanf("%d %d", &head, &N);
    int32_t key_count[max_key] = {0};
    std::vector<int32_t> remove_ans;
    for (int32_t i = 0; i != N; ++i)
    {
        Node _tmp;
        scanf("%d %d %d", &_tmp.addr, &_tmp.key, &_tmp.next);
        /* 忽视重复地址 */
        node_pack[_tmp.addr] = _tmp;
        key_count[_abs(_tmp.key)] += 1;
    }
    //std::cout << "==============="<<std::endl;
    int tail = -1;
    while (head != -1)
    {
        if (key_count[_abs(node_pack[head].key)] == 0)
        {
            remove_ans.push_back(head);
        }
        else
        {
            if (tail != -1)
            {
                printf(" %05d\n", tail);
            }
            printf("%05d %d", head, node_pack[head].key);
            key_count[_abs(node_pack[head].key)] = 0;
        }
        head = node_pack[head].next;
        tail = head;
        if (tail == -1)
        {
            printf(" -1\n");
        }
    }
    tail = -1;
    for (int idx = 0; idx != remove_ans.size(); ++idx)
    {
        if (tail != -1)
        {
            printf(" %05d\n", tail);
        }
        printf("%05d %d", node_pack[remove_ans[idx]].addr, node_pack[remove_ans[idx]].key);
        if (idx + 1 == remove_ans.size())
        {
            printf(" -1\n");
            break;
        }
        tail = node_pack[remove_ans[idx+1]].addr;
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值