zoukankan      html  css  js  c++  java
  • pat1097. Deduplication on a Linked List (25)

    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 #include<cstdio>
     2 #include<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 struct ndoe{
    10     int k,next;
    11 };
    12 ndoe mem[100005];
    13 bool ha[10005];
    14 int abs(int a){
    15     if(a<0){
    16         a=-a;
    17     }
    18     return a;
    19 }
    20 #define refirst 100001
    21 #define nrfirst 100002
    22 int main(){
    23     //freopen("D:\INPUT.txt","r",stdin);
    24     int n,first;
    25     scanf("%d %d",&first,&n);
    26     int i,cur;
    27     for(i=0;i<n;i++){
    28         scanf("%d",&cur);
    29         scanf("%d %d",&mem[cur].k,&mem[cur].next);
    30     }
    31     cur=nrfirst;
    32     int relist=refirst,p=first;
    33     while(p!=-1){
    34         while(p!=-1&&ha[abs(mem[p].k)]){
    35             mem[relist].next=p;
    36             relist=p;
    37             p=mem[p].next;
    38         }
    39         if(p!=-1){
    40             ha[abs(mem[p].k)]=true;
    41         }
    42         mem[cur].next=p;
    43         if(p!=-1){
    44             cur=p;
    45             p=mem[cur].next;
    46         }
    47     }
    48     mem[relist].next=-1;
    49     p=mem[nrfirst].next;
    50     while(p!=-1){
    51         if(mem[p].next!=-1){
    52             printf("%05d %d %05d",p,mem[p].k,mem[p].next);
    53         }
    54         else{
    55             printf("%05d %d %d",p,mem[p].k,mem[p].next);
    56         }
    57         printf("
    ");
    58         p=mem[p].next;
    59     }
    60     p=mem[refirst].next;
    61     while(p!=-1){
    62         if(mem[p].next!=-1){
    63             printf("%05d %d %05d",p,mem[p].k,mem[p].next);
    64         }
    65         else{
    66             printf("%05d %d %d",p,mem[p].k,mem[p].next);
    67         }
    68         printf("
    ");
    69         p=mem[p].next;
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    mysql的查询、子查询及连接查询
    面向对象知识点续及单例模式
    PSR-2 编码风格规范
    PSR-1 基础编码规范
    博客园文章添加版权信息的方法
    微博第三方登陆接入流程
    使用EasyWechat快速开发微信公众号支付
    Git版本管理工具的使用
    E: Could not get lock /var/lib/apt/lists/lock
    ubuntu安装opencv
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4781207.html
Copyright © 2011-2022 走看看