zoukankan      html  css  js  c++  java
  • 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 (<= 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<iostream>
     3 #include<algorithm>
     4 #include<map>
     5 using namespace std;
     6 typedef struct NODE{
     7     int lt, rt;
     8     int data;
     9     int rank;
    10     int valid;
    11     NODE(){
    12         valid = -1;  //分为3类。不重复的合法数据为1,重复的合法数据为0,非法数据为-1
    13     }
    14 }node;
    15 node nds[100001];
    16 map<int, int> mp;
    17 bool cmp(node a, node b){
    18     if(a.valid != b.valid)
    19         return a.valid > b.valid;
    20     else return a.rank < b.rank;
    21 }
    22 int main(){
    23     int N, head;
    24     scanf("%d%d", &head, &N);
    25     int addr;
    26     for(int i = 0; i < N; i++){
    27         scanf("%d", &addr);
    28         nds[addr].lt = addr;
    29         scanf("%d %d", &nds[addr].data, &nds[addr].rt);
    30     }
    31     int pt = head, index = 0;
    32     while(pt != -1){
    33         if(mp.count(abs(nds[pt].data)) == 0){
    34             mp[abs(nds[pt].data)] = 0;
    35             nds[pt].valid = 1;
    36         }else{
    37             nds[pt].valid = 0;
    38         }
    39         nds[pt].rank = index++;
    40         pt = nds[pt].rt;
    41     }
    42     sort(nds, nds + 100001, cmp);
    43     int dele = -1;
    44     for(int i = 0; i < index; i++){
    45         if(nds[i].valid != 1){
    46             dele = i;
    47             break;
    48         }
    49     }
    50     if(dele == -1)
    51         dele = 0;
    52     for(int i = 0; i < dele; i++){
    53         if(i < dele - 1)
    54             printf("%05d %d %05d
    ", nds[i].lt, nds[i].data, nds[i + 1].lt);
    55         else printf("%05d %d -1
    ", nds[i].lt, nds[i].data);
    56     }
    57     for(int i = dele; i < index; i++){
    58         if(i < index - 1)
    59             printf("%05d %d %05d
    ", nds[i].lt, nds[i].data, nds[i + 1].lt);
    60         else printf("%05d %d -1
    ", nds[i].lt, nds[i].data);
    61     }
    62     cin >> N;
    63     return 0;
    64 }
    View Code

    总结:

    1、在遍历合法链表时给每个节点编号,之后按照编号进行排序,既可保持链表顺序,又方便输出。

    2、给链表节点打不同的标记,再利用排序sort的分类的特点,可以对链表的节点进行分类。

    3、利用 map 来记录某个节点的data是否在之前存在过。

  • 相关阅读:
    Spring配置文件中的那些标签意味着什么(持续更新)
    转 spring配置文件
    Web.xml配置详解之context-param
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    spring mvc 中web.xml配置信息解释
    转 一个web项目web.xml的配置中<context-param>配置作用
    在web.xml中通过contextConfigLocation配置spring
    (转)web.xml中的contextConfigLocation在spring中的作用
    Android菜鸟的成长笔记(20)——IntentService
    PhotoSwipe源码解读系列(二)
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8531703.html
Copyright © 2011-2022 走看看