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;
    }
    
  • 相关阅读:
    【Express系列】第3篇——接入mysql
    【Express系列】第2篇——主程序的改造
    【Express系列】第1篇——项目创建
    AngularJS内置指令
    node服务端搭建学习笔记
    生成ssh key
    webstorm的常用操作
    VSCode 常用插件
    php集成包
    composer安装特别慢的解决方案
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13599699.html
Copyright © 2011-2022 走看看