1. 程式人生 > >PAT1097:Deduplication on a Linked List

PAT1097:Deduplication on a Linked List

adr tput each gpo 文本對比 for each lis pie only

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\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
            }
            else
            {
                newlist[i - 1].next = newlist[i].address;
                printf("%05d %d %05d\n",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\n",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\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
            }
        }
    }
}

  

PAT1097:Deduplication on a Linked List