【PAT】【Advanced Level】1097. Deduplication on a Linked List (25)
2017-09-06 18:49
316 查看
1097. Deduplication on a Linked List (25)
时间限制300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1097
思路:
输入,map映射地址、下标
然后根据map从起始地址开始串成链表,同时检查绝对值情况
CODE:
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<cmath>
#define N 100010
using namespace std;
typedef struct S
{
int ad;
int va;
int nx;
};
S st
;
map<int,int> ma1;
map<int,int> ma2;
vector<S> com;
vector<S> oth;
int main()
{
int n;
int bg;
scanf("%d %d",&bg,&n);
//cout<<bg<<" "<<n<<endl;
for (int i=0;i<n;i++)
{
scanf("%d %d %d",&st[i].ad,&st[i].va,&st[i].nx);
ma1[st[i].ad]=i;
}
while (bg!=-1)
{
if (ma2[abs(st[ma1[bg]].va)]==0)
{
ma2[abs(st[ma1[bg]].va)]=1;
com.push_back(st[ma1[bg]]);
}
else
{
oth.push_back(st[ma1[bg]]);
}
bg=st[ma1[bg]].nx;
}
for (int i=0;i<com.size();i++)
{
printf("%05d %d ",com[i].ad,com[i].va);
if (i!=com.size()-1)
{
printf("%05d\n",com[i+1].ad);
}
else
printf("-1\n");
}
for (int i=0;i<oth.size();i++)
{
printf("%05d %d ",oth[i].ad,oth[i].va);
if (i!=oth.size()-1)
{
printf("%05d\n",oth[i+1].ad);
}
else
printf("-1\n");
}
return 0;
}
相关文章推荐
- 【C++】PAT (advanced level)1074. Reversing Linked List (25)
- PAT (Advanced Level) 1074. Reversing Linked List (25)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- PAT (Advanced Level) Practise 1074 Reversing Linked List (25)
- PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
- PAT (Advanced Level) Practise 1097 Deduplication on a Linked List (25)
- PAT Advanced Level 1074. Reversing Linked List (25)
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25) 链表去重
- PAT (Advanced Level) Practise 1097 Deduplication on a Linked List (25)
- PAT (Advanced Level) Practise 1133 Splitting A Linked List (25)
- 1074. Reversing Linked List (25)【链表翻转】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1074 Reversing Linked List (25)
- 【PAT】【Advanced Level】1074. Reversing Linked List (25)
- PAT (Advanced Level) 1074. Reversing Linked List (25) 翻转链表
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
- Pat(Advanced Level)Practice--1074(Reversing Linked List)
- PAT (Advanced) 1074. Reversing Linked List (25)
- PAT (Advanced Level) Practise 1047 Student List for Course (25)
- 【C++】PAT(advanced level)1039. Course List for Student (25)