zoukankan      html  css  js  c++  java
  • PAT甲级——1097 Deduplication on a Linked List (链表)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982

    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 Lbeing 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

    题目大意:从头结点开始遍历一个链表,将遇到的节点的key的绝对值标记,若重复,则将此节点放到另一个链表里;举个例子,List  21→-15→-15→-7→15,答案是 21→-15→-7 和 -15→15 两个链表。

    思路:链表的节点地址是5位整数,-1表示Null ,用数组存放链表。基础的链表删减操作(其实只要更改相应节点的next地址),用 pre 记录前一个节点的地址,addr 记录当前节点的地址,数组也好、set也好、map也好,设置一个容器用于标记已经出现过的abs( key )。

     1 #include <iostream>
     2 #include <unordered_map>
     3 #include <vector>
     4 #include <cmath>
     5 #define MaxNum 100001
     6 using namespace std;
     7 
     8 struct node {
     9     int key, next = -1;
    10 };
    11 
    12 vector <node> List(MaxNum);
    13 unordered_map <int, bool> S;
    14 
    15 int main()
    16 {
    17     int head, N;
    18     scanf("%d%d", &head, &N);
    19     for (int i = 0; i < N; i++) {
    20         int addr;
    21         scanf("%d", &addr);
    22         scanf("%d%d", &List[addr].key, &List[addr].next);
    23     }
    24     int pre = head, addr = List[head].next, secHead = -1, secAddr;
    25     S[abs(List[head].key)] = true;
    26 
    27     while (addr != -1) {
    28         if (S[abs(List[addr].key)]) {
    29             List[pre].next = List[addr].next;
    30             if (secHead == -1) {
    31                 secHead = addr;
    32                 secAddr = secHead;
    33                 List[secHead].next = -1;
    34             }
    35             else {
    36                 List[secAddr].next = addr;
    37                 secAddr = addr;
    38                 List[secAddr].next = -1;
    39             }
    40             addr = List[pre].next;
    41         }
    42         else {
    43             S[abs(List[addr].key)] = true;
    44             pre = addr;
    45             addr = List[addr].next;
    46         }
    47     }
    48     for (addr = head; addr != -1; addr = List[addr].next) {
    49         printf("%05d %d ", addr, List[addr].key);
    50         List[addr].next == -1 ? printf("-1
    ") : printf("%05d
    ", List[addr].next);
    51     }
    52     for (secAddr = secHead; secAddr != -1; secAddr = List[secAddr].next) {
    53         printf("%05d %d ", secAddr, List[secAddr].key);
    54         List[secAddr].next == -1 ? printf("-1
    ") : printf("%05d
    ", List[secAddr].next);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    w3wp.exe占用CPU100%的解决办法
    Visual Studio 2005 查找和替换窗口 显示不了
    IIS:w3wp.exe进程占用cpu和内存过多的处理办法
    C# form ComboBox
    从尾到头打印链表,不允许逆置原链表
    [置顶] ATL窗口thunk机制的剖析与实现
    flex自定义用ArrayCollection做数据源的带checkbox的tree(功能强大的完美版^_^)
    oracle的PremaredStatement.executeBatch为什么返回2
    窗体Controls的OfType<>方法的使用
    HDU 1421 动态规划(DP) 搬寝室
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/10989368.html
Copyright © 2011-2022 走看看