1097. Deduplication on a Linked List

来源:互联网 发布:中走丝hf上下异形编程 编辑:程序博客网 时间:2024/06/16 22:57

题目如下,重点内容加粗
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.将链表中重复的节点去掉。重复的节点指的是节点值得绝对值相等。
2.去掉的节点放入一个单独的链表中。
3.将不重复的链表和重复的链表输出。


题目易错点如下
1.判断重复时,要按链表原来的顺序。即 按构建的链表的顺序一个一个判断 ,而不是看到一个节点,去查询该节点后面的所有节点是否值的绝对值相同。举个例子:12->9->10->9->12。
对于该链表,正确过程如下,其中list1代表不重复的链表,list2代表重复节点的链表。
(1)12,list1中没有值12的,加入list1。结果:list1:12 list2:null
(2)9,list1中没有值9的,加入list2。结果:list1:12->9 list2:null
(3)10,list1中没有值10的,加入list2。结果:list1:12->9->10 list2:null
(4)9,list1中存在,加入list2。结果:list1:12->9->10 list2:9
(5)12,list1中存在,加入list2。结果:list1:12->9->10 list2:9->12
错误过程如下
(1)12,list1中没有,加入list1。从下一个节点开始判断有无重复的:9->10->9->12,最后一个节点重复,加入list2。结果 list1:12 list2:12
(2)9,list1中没有,加入list1。从下一个节点开始判断有无重复的:10->9->12,其中9重复,加入list2。结果:list1:12->9 list2:12->9
……
2.输入first=-1时,直接程序结束。


解题过程如下
1.判断链表是否存在,不存在程序结束。
2.建立链表
3.去掉重复的,我用map暂存节点的值


程序如下

#include <iostream>#include <stdio.h>#include <vector>#include <math.h>#include <map>/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;struct node{    int add;    int value;    int next;};int main(int argc, char *argv[]) {    int first,n;    scanf("%d%d",&first,&n);    if(first==-1){        return 0;    }    node mynode[100010];    vector<node> myvector;    vector<node> goodvector;    vector<node> remainvector;    map<int,int> map1;    for(int i=0;i<n;i++){        int add,value,next;        scanf("%d%d%d",&add,&value,&next);        mynode[add].add = add;        mynode[add].value = value;        mynode[add].next = next;    }    while(first!=-1){        int next = mynode[first].next;        myvector.push_back(mynode[first]);        first = next;       }    if(size==1){        printf("%05d %d %d\n",myvector[0].add,myvector[0].value,-1);    }    else if(size>1){//  for(int i=0;i<size;i++){//      node t = myvector[i];//      int value = abs(t.value);//      if(value<=10000){//          goodvector.push_back(t);//          myvector[i].value = 100000;//          int j = i+1;//          if(i!=size-1){//              for(;j<size;j++){//          node t1 = myvector[j];//          int value1 = abs(t1.value);//          if(value==value1){//              remainvector.push_back(t1);//              myvector[j].value=100000;//          }//          }//          }//      }//  }    for(int i=0;i<size;i++){        node t = myvector[i];        int value = abs(t.value);        if(map1.count(value)==1){            remainvector.push_back(t);//存在key,加入remainvector         }        else{//不存在key,加入goodvector             goodvector.push_back(t);            map1[value] = value;        }       }    if(goodvector.size()>0){    for(int i=0;i<goodvector.size()-1;i++){        printf("%05d %d %05d\n",goodvector[i].add,goodvector[i].value,goodvector[i+1].add);    }    printf("%05d %d %d\n",goodvector[goodvector.size()-1].add,goodvector[goodvector.size()-1].value,-1);    }    if(remainvector.size()>0){    for(int i=0;i<remainvector.size()-1;i++){        printf("%05d %d %05d\n",remainvector[i].add,remainvector[i].value,remainvector[i+1].add);    }    printf("%05d %d %d\n",remainvector[remainvector.size()-1].add,remainvector[remainvector.size()-1].value,-1);    }}       return 0;}
原创粉丝点击