zoukankan      html  css  js  c++  java
  • 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) 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 #include<stdio.h>
     2 #include<math.h>
     3 #include<set>
     4 #include<algorithm>
     5 #include<vector>
     6 using namespace std;
     7 struct node
     8 {
     9     int val;
    10     int next,id;
    11 };
    12 node Link[100100];
    13 int main()
    14 {
    15     int head,n,id,val,next;
    16     scanf("%d%d",&head,&n);
    17     for(int i = 0 ;i < n ;++i)
    18     {
    19         scanf("%d%d%d",&id,&val,&next);
    20         Link[id].val = val;
    21         Link[id].next = next;
    22         Link[id].id = id;
    23     }
    24     int p = head;
    25     set<int> ss;
    26     vector<node> vv,vv2;
    27     while(p != -1)
    28     {
    29         int tem = abs(Link[p].val);
    30         if(ss.count(tem) == 0)
    31         {
    32             ss.insert(tem);
    33             vv.push_back(Link[p]);
    34         }
    35         else vv2.push_back(Link[p]);
    36         p = Link[p].next;
    37     }
    38     p = head;
    39     for(int i = 0 ; i < (int)vv.size() - 1;++i)
    40     {
    41         printf("%05d %d %05d
    ",vv[i].id,vv[i].val,vv[i+1].id);
    42     }
    43     if(vv.size() > 0)
    44         printf("%05d %d -1
    ",vv[vv.size()-1].id,vv[vv.size()-1].val);
    45 
    46     for(int i = 0 ;i < (int)vv2.size() -1;++i)
    47     {
    48         printf("%05d %d %05d
    ",vv2[i].id,vv2[i].val,vv2[i+1].id);
    49     }
    50     if(vv2.size() > 0)
    51         printf("%05d %d -1
    ",vv2[vv2.size()-1].id,vv2[vv2.size()-1].val);
    52     return 0;
    53 }
  • 相关阅读:
    TreeView 控件选中项变色
    遍历repeater
    堆和栈,类型声明实例化过程,string=null;string=”“的区别;
    用Oled操作EXCEL碰到错误
    修改部分代码,让优酷和ckplayer可覆盖,优酷播放的部分API。
    C# 拼接字符串,自实现翻页功能
    总结Vue第二天:自定义子组件、父子组件通信、插槽
    总结Vue第一天:简单介绍、基本常用知识、辅助函数
    总结Vue第三天:模块化和webpack模块化打包:
    PHP之冒泡排序
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5213964.html
Copyright © 2011-2022 走看看