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 (<= 10^5^) 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 10^4^, 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

    题目大意:给出一些节点信息; 要求把具有不同绝对值的节点组合成一条链表, 绝对值和之前相同的结点组合成另外一条链表
    思路:用vector<int> v来保存当前节点指向下一节点的指针, vector<int> value 保存当前节点的值, vector<int> exist保存数值的出现情况; 从头结点开始遍历,根据是否重复把节点添加到不同的链表中; 输出的时候,更改节点的指针

     1 #include<iostream>
     2 #include<vector>
     3 #include<math.h>
     4 using namespace std;
     5 struct node{
     6   int addr, val, next;
     7 };
     8 int main(){
     9   int root, n, i;
    10   cin>>root>>n;
    11   vector<int>  v(100000, -1), value(100000);
    12   int addr, val, next, r;
    13   bool flag=true;
    14   for(i=0; i<n; i++){
    15     scanf("%d %d %d", &addr, &val, &next);
    16     v[addr]=next;
    17     value[addr]=val;
    18   }
    19   vector<node> l1, l2;
    20   node nnode;
    21   vector<int> exist(10010, -1);
    22   while(true){
    23     if(root==-1) break;
    24     nnode.addr=root; nnode.val=value[root]; 
    25     int temp=abs(value[root]);
    26     if(exist[temp]==-1){l1.push_back(nnode); exist[temp]=1;}
    27     else l2.push_back(nnode);
    28     root=v[root];
    29   }
    30   for(i=0; i<l1.size(); i++){
    31     if(i!=l1.size()-1) printf("%05d %d %05d
    ", l1[i].addr, l1[i].val, l1[i+1].addr);
    32     else printf("%05d %d %d
    ", l1[l1.size()-1].addr, l1[l1.size()-1].val, -1);
    33   }
    34   for(i=0; i<l2.size(); i++){
    35     if(i!=l2.size()-1) printf("%05d %d %05d
    ", l2[i].addr, l2[i].val, l2[i+1].addr);
    36     else printf("%05d %d %d
    ", l2[l2.size()-1].addr, l2[l2.size()-1].val, -1);
    37   }
    38   return 0;
    39 }

    pat中链表类的题目大多都是这种模式,使用的还不是很熟练还需要多理解

    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    android获得屏幕高度和宽度
    Android中RelativeLayout各个属性的含义
    Android JNI知识简介
    Android结束进程方法
    android开发中的图片资源问题
    android framework下public.xml中添加资源问题
    I'm back
    test,exec,match,replace方法区别 正则
    fiddler抓包工具 https抓取 ios手机端抓取
    前端优化方案
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9167109.html
Copyright © 2011-2022 走看看