zoukankan      html  css  js  c++  java
  • PAT甲级——A1097 Deduplication on a Linked List

    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 (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

    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 1, 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 #include <iostream>
     2 #include <unordered_map>
     3 #include <cmath>
     4 using namespace std;
     5 struct Node
     6 {
     7     int addr, val;
     8     Node(int a, int b) :addr(a), val(b) {}
     9 };
    10 struct List
    11 {
    12     Node val;
    13     List* next;
    14     List(Node a) :val(a), next(nullptr) {}
    15 };
    16 int N, head, addr1, addr2, val, numbers[100100] = { 0 };
    17 int main()
    18 {
    19     cin >> head >> N;
    20     if (head == -1)//切记,此时有一个测试例子是头结点为-1,那么什么也不输出
    21         return 0;
    22     unordered_map<int, pair<int, int>>map;
    23     for (int i = 0; i < N; ++i)
    24     {
    25         cin >> addr1 >> val >> addr2;
    26         map[addr1] = make_pair(val, addr2);
    27     }
    28     //将链表组合起来
    29     List* resHead = new List(Node(0, -1));
    30     List* delHead = new List(Node(0, -1));
    31     List* p = resHead;
    32     while (head != -1)
    33     {
    34         List* q = new List(Node(head, map[head].first));
    35         p->next = q;
    36         p = q;
    37         head = map[head].second;
    38     }
    39     List* pre = resHead, *delP = delHead;
    40     p = pre->next;
    41     while (p != nullptr)
    42     {
    43         if (numbers[abs(p->val.val)] == 1)
    44         {
    45             pre->next = p->next;//删除
    46             List* q = new List(Node(p->val.addr, p->val.val));
    47             delP->next = q;
    48             delP = q;
    49             delete p;
    50             p = pre->next;
    51         }
    52         else
    53         {
    54             numbers[abs(p->val.val)]++;
    55             pre = p;
    56             p = pre->next;
    57         }
    58     }
    59     p = resHead->next;
    60     while (p != nullptr)
    61     {
    62         printf("%05d %d ", p->val.addr, p->val.val);    
    63         p = p->next;
    64         if (p == nullptr)
    65             printf("%d
    ", -1);
    66         else
    67             printf("%05d
    ", p->val.addr);
    68     }
    69     p = delHead->next;
    70     while (p != nullptr)
    71     {
    72         printf("%05d %d ", p->val.addr, p->val.val);
    73         p = p->next;
    74         if (p == nullptr)
    75             printf("%d
    ", -1);
    76         else
    77             printf("%05d
    ", p->val.addr);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    Starlink星链计划能与5G抗衡?看一下马斯克吹过的牛逼
    代码安全性和健壮性:如何在if和assert中做选择?
    都说软件架构要分层、分模块,具体应该怎么做(一)
    物联网网关开发:基于MQTT消息总线的设计过程(上)
    Linq 集成化查询(1)
    给自己定位的技术总监
    lucene.net初接触
    人生就像一系统软件
    用Microsoft.Practices.Unity实现简单的依赖注入
    flv播放器参数
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11356591.html
Copyright © 2011-2022 走看看