zoukankan      html  css  js  c++  java
  • 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
    ",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
    ");
        //处理sep
        if(sepadd.size()!=0){
            count = 1;
            for(int i = 0;i<sepadd.size();i++){
                if(count!=1) printf("%05d
    ",sepadd[i]);
                printf("%05d %d ",sepadd[i],node[sepadd[i]].key);
                count++;
            }
            printf("-1
    ");        
        }
    
        return 0;
    }
    
  • 相关阅读:
    51nod 1031+斐波那契和杨辉三角的一些基础知识
    51nod 1297
    萌新二叉树学习笔记
    HDU3415【单调队列】
    萌新瞎讲网络流之最大流【不定期更新理解篇】
    萌新浅谈单调队列
    51nod 1021【区间DP】
    51nod 1278【贪心】
    51nod 1413
    51nod1181【素数筛】
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13599699.html
Copyright © 2011-2022 走看看