L2-002. 链表去重
来源:互联网 发布:linux查看telnet服务 编辑:程序博客网 时间:2024/06/09 21:03
L2-002. 链表去重
时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点。即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留。同时,所有被删除的结点必须被保存在另外一个链表中。例如:另L为21→-15→-15→-7→15,则你必须输出去重后的链表21→-15→-7、以及被删除的链表-15→15。
输入格式:
输入第一行包含链表第一个结点的地址、以及结点个数N(<= 105 的正整数)。结点地址是一个非负的5位整数,NULL指针用-1表示。
随后N行,每行按下列格式给出一个结点的信息:
Address Key Next
其中Address是结点的地址,Key是绝对值不超过104的整数,Next是下一个结点的地址。
输出格式:
首先输出去重后的链表,然后输出被删除结点组成的链表。每个结点占一行,按输入的格式输出。
输入样例:00100 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854输出样例:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
这题直接模拟,开了三个结构体数组,分别放原始数据,去重连表,和多余链表。
#include<iostream>#include<string>#include<cmath>#include<cstdio>using namespace std;struct node{long id;int key;long next;}a[100001],b[100001],c[100001];int flag[100001]={0};int main(){long n,ad;cin>>ad>>n;while(n--){long i,j;cin>>i;a[i].id=i;cin>>a[i].key>>j;a[i].next=j;}long k=0,l=0;long temp=ad;//for(int i=0;i<n;i++)while(a[temp].next!=-1){if(!flag[abs(a[temp].key)]){flag[abs(a[temp].key)]=1;b[k++].id=a[temp].id;b[k-1].key=a[temp].key;b[k-1].next=a[temp].next;}else{c[l++].id=a[temp].id;c[l-1].key=a[temp].key;c[l-1].next=a[temp].next;}temp=a[temp].next;}if(!flag[abs(a[temp].key)]) //后面这两个if和while里面的一样,是因为前面while里面还有最后一个没 // 有处理,就直接再加一个判断了,可以优化。{flag[abs(a[temp].key)]=1;b[k++].id=a[temp].id;b[k-1].key=a[temp].key;b[k-1].next=a[temp].next;}else{c[l++].id=a[temp].id;c[l-1].key=a[temp].key;c[l-1].next=a[temp].next;}for(int i=0;i<k;i++){if(i==k-1){b[i].next=-1;}elseb[i].next=b[i+1].id;}for(int i=0;i<l;i++){if(i==l-1){c[i].next=-1;}elsec[i].next=c[i+1].id;}for(int i=0;i<k;i++){printf("%05ld",b[i].id);cout<<" "<<b[i].key<<" ";if(b[i].next==-1)cout<<-1<<endl;elseprintf("%05ld\n",b[i].next);}for(int i=0;i<l;i++){printf("%05ld",c[i].id);cout<<" "<<c[i].key<<" ";if(c[i].next==-1)cout<<-1<<endl;elseprintf("%05ld\n",c[i].next);}return 0;}
1 0
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- mybatis 打印执行sql语句l
- 禅道-从windows7迁移至ubuntu14.04之五
- RTP 协议
- android调用系统相册
- Lucene&Solr(之二)-索引库CRUD、Solr的安装
- L2-002. 链表去重
- 一个主机nginx绑定多个SSL证书(https)
- 微信的发展堪称疯狂,这 7 点值得所有人学习|投资人说-20170504早读课
- private static final long serialVersionUID = 5461344781588144485L;是什么意思,后面的号码是如何生成的
- bzoj2219 数论之神
- Codeforces gym 101102 K 想法
- BlueROV-6: Quick Start of Dronekit and Connect Dronekit to the Vehicle
- 设计模式_组合模式
- Jfinal中使用EhCache缓存