zoukankan      html  css  js  c++  java
  • PAT1097:Deduplication on a Linked List

    1097. Deduplication on a Linked List (25)

    时间限制
    300 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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) 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, 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

    思路
    1.map模拟一个链表dic存放输入的所有的节点,set模拟一个dupdic记录一个节点的值来判断接下来的节点是否由重复的key值,两个vector分别为去重链表和重复节点链表。
    2.遍历map,根据set容器内的key值决定是将当前节点插入去重链表还是重复节点链表,并不断更新set存放的key值。
    3.对两个vector新链表的节点的next地址赋予它下个节点的地址。
    4.遍历两个链表并依次输出即可,
    代码
    #include<iostream>
    #include<vector>
    #include<map>
    #include<set>
    #include<math.h>
    using namespace std;
    class node
    {
    public:
        int address;
        int key;
        int next;
    };
    
    map<int,node> dic;
    set<int> dupdic;
    vector<node> newlist;
    vector<node> removed_list;
    
    int main()
    {
        int head,n;
        while(cin >> head >> n)
        {
            //input
            for(int i = 0;i < n;i++)
            {
                int adr;
                cin >> adr;
                dic[adr].address = adr;
                cin >> dic[adr].key >> dic[adr].next;
            }
            //handle
            while(head != -1)
            {
                if(dupdic.find(abs(dic[head].key)) != dupdic.end())
                {
                    removed_list.push_back(dic[head]);
                }
                else
                {
                    newlist.push_back(dic[head]);
                    dupdic.insert(abs(dic[head].key));
                }
                head = dic[head].next;
            }
            int lenn = newlist.size(),lenr = removed_list.size();
            //output
            for(int i = 1;i <= lenn;i++)
            {
                if(i == lenn)
                {
                    newlist[i - 1].next = -1;
                    printf("%05d %d %d
    ",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
                }
                else
                {
                    newlist[i - 1].next = newlist[i].address;
                    printf("%05d %d %05d
    ",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
                }
            }
            for(int i = 1;i <= lenr;i++)
            {
                if(i == lenr)
                {
                    removed_list[i - 1].next = -1;
                    printf("%05d %d %d
    ",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
                }
                else
                {
                    removed_list[i - 1].next = removed_list[i].address;
                    printf("%05d %d %05d
    ",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
                }
            }
        }
    }
    

      

  • 相关阅读:
    国际商务金融
    [手游新项目历程]第22天-野指针
    疯狂的A股:“杠杆牛市”后,是否坠入新轮回?
    [硬盘分区]EASEUS怎么用(EASEUS Partition Master使用教程)
    [硬盘分区]EASEUS怎么用(EASEUS Partition Master使用教程)
    linux下如何更新glibc包
    游戏是任何行业的垂直行业,《游戏改变世界》读后感
    虚函数实现原理(转)
    虚函数实现原理(转)
    bzoj2783
  • 原文地址:https://www.cnblogs.com/0kk470/p/8143150.html
Copyright © 2011-2022 走看看