标签:advance example result code strong file rem equal NPU
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 (<= 105) 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 104, 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
已知N个结点,对链表中结点data绝对值相同的结点进行删除(保留绝对值相同第一次出现的结点),分别打印链表中留下的结点,链表中删除的结点
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
int order=2*maxn;
} nds[maxn];
bool exist[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char * argv[]) {
int hadr,n,adr;
scanf("%d %d",&hadr,&n);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int cnt1=0,cnt2=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
if(exist[abs(nds[i].data)]) {
nds[i].order=maxn+cnt2++;
} else {
exist[abs(nds[i].data)]=true;
nds[i].order=cnt1++;
}
}
sort(nds,nds+maxn,cmp);
int cnt=cnt1+cnt2;
for(int i=0; i<cnt; i++) {
printf("%05d %d",nds[i].adr,nds[i].data);
if(i==cnt1-1||i==cnt-1)printf(" -1\n");
else printf(" %05d\n",nds[i+1].adr);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
int order=maxn;
} nds[maxn];
bool exist[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char * argv[]) {
int hadr,n,adr;
scanf("%d %d",&hadr,&n);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int count=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
nds[i].order=count++;
}
n=count;
sort(nds,nds+maxn,cmp);
vector<node> r;
vector<node> d;
for(int i=0; i<count; i++) {
if(exist[abs(nds[i].data)]) {
d.push_back(nds[i]);
} else {
exist[abs(nds[i].data)]=true;
r.push_back(nds[i]);
}
}
for(int i=0; i<r.size(); i++) {
printf("%05d %d",r[i].adr,r[i].data);
if(i==r.size()-1)printf(" -1\n");
else printf(" %05d\n",r[i+1].adr);
}
for(int i=0; i<d.size(); i++) {
printf("%05d %d",d[i].adr,d[i].data);
if(i==d.size()-1)printf(" -1\n");
else printf(" %05d\n",d[i+1].adr);
}
return 0;
}
PAT Advanced 1097 Deduplication on a Linked List (25) [链表]
标签:advance example result code strong file rem equal NPU
原文地址:https://www.cnblogs.com/houzm/p/12297285.html