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

题目大意:给出一个静态链表,将绝对值出现过的节点按照出现顺序组成另一个链表,从原链表中删除。输出删除后剩余的链表,再输出删除的链表。

分析:从根节点开始遍历链表,并记录出现过的节点的val值。每次遍历到一个节点,先检查它的值是否出现过,没出现过则记录,出现过则删除。最后分别输出。

cpp 复制代码
#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
using namespace std;

typedef struct node
{
    int id,val,next;
}node;
node num[100005],news[100005],del[100005];

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int root,m;scanf("%d%d",&root,&m);
    int cnt[100005]={0};
    for(int i=0;i<m;++i)
    {
        int id,val,next;scanf("%d%d%d",&id,&val,&next);
        num[id].id=id,num[id].val=val,num[id].next=next;
    }
    int ends=num[root].next,ln=1,ld=0;
    news[0]=num[root];
    int sum=num[root].val;
    if(sum<0)sum*=-1;
    cnt[sum]=1;
    while(ends!=-1)
    {
        int temp=num[ends].val;
        if(temp<0)temp=temp*-1;
        if(cnt[temp]==0)news[ln++]=num[ends],cnt[temp]++;
        else del[ld++]=num[ends];
        ends=num[ends].next;
    }
    for(int i=0;i<ln;++i)
    {
        printf("%05d %d ",news[i].id,news[i].val);
        if(i+1<ln)printf("%05d\n",news[i+1].id);
        else printf("-1\n");
    }
    for(int i=0;i<ld;++i)
    {
        printf("%05d %d ",del[i].id,del[i].val);
        if(i+1<ld)printf("%05d\n",del[i+1].id);
        else printf("-1\n");
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}
相关推荐
圣保罗的大教堂12 天前
1089 Insert or Merge (25)
pat考试
圣保罗的大教堂15 天前
1081 Rational Sum (20)
pat考试
圣保罗的大教堂15 天前
1083 List Grades (25)
pat考试
圣保罗的大教堂19 天前
1074 Reversing Linked List (25)
pat考试
圣保罗的大教堂1 个月前
1052 Linked List Sorting (25)
pat考试
圣保罗的大教堂1 个月前
1047 Student List for Course (25)
pat考试
圣保罗的大教堂2 个月前
1028 List Sorting (25)
pat考试
哈哈,柳暗花明2 个月前
软件设计师笔记-数据结构
pat考试
绯樱殇雪2 个月前
编程题 7-29 删除字符串中的子串【PAT】
c++·pat考试