PAT A1097 Deduplication on a Linked List (25分)

测试点2是重复数字的链表为空,此时不需要输出-1,所以需要把seq处理包装起来
而如果第一个非重复链表为空,需要输出-1

#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int N = 100010;
struct Node{
    int address,key,next;
    bool isvis = false;
}node[N];
set<int> hasabs;
vector<int> sepadd;//存放不要的地址
int main(){
    int n,start;
    scanf("%d %d",&start,&n);
    int waitaddress;
    for(int i = 0;i<n;i++){
        int taddress,tkey,tnext;
        scanf("%d %d %d",&taddress,&tkey,&tnext);
        node[taddress].address = taddress;
        node[taddress].key = tkey;
        node[taddress].isvis = true;
        node[taddress].next = tnext;
    }
    int count =1;
    while(start!=-1){
        if(hasabs.find(abs(node[start].key))==hasabs.end()){
            if(count!=1) printf("%05d\n",start);//保留5位
            hasabs.insert(abs(node[start].key));
            printf("%05d %d ",start,node[start].key);
            
        }else{
            sepadd.push_back(node[start].address);
        }
        start = node[start].next;
        count++;
    }
    printf("-1\n");
    //处理sep
    if(sepadd.size()!=0){
        count = 1;
        for(int i = 0;i<sepadd.size();i++){
            if(count!=1) printf("%05d\n",sepadd[i]);
            printf("%05d %d ",sepadd[i],node[sepadd[i]].key);
            count++;
        }
        printf("-1\n");        
    }

    return 0;
}
posted @ 2020-09-02 02:42  是水泵呢  阅读(165)  评论(0编辑  收藏  举报