【PAT甲级】1097 Deduplication on a Linked List (25 分)

题意:

输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址。输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表)。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int nex[100007],val[100007];
 5 vector<pair<int,int> >ans,ans2;
 6 int vis[100007];
 7 int main(){
 8     //ios::sync_with_stdio(false);
 9     //cin.tie(NULL);
10     //cout.tie(NULL);
11     int s,n;
12     cin>>s>>n;
13     for(int i=1;i<=n;++i){
14         int x,y,z;
15         cin>>x>>y>>z;
16         nex[x]=z;
17         val[x]=y;
18     }
19     while(s!=-1){
20         if(vis[abs(val[s])]){
21             ans2.push_back({s,val[s]});
22             s=nex[s];
23             continue;
24         }
25         ans.push_back({s,val[s]});
26         vis[abs(val[s])]=1;
27         s=nex[s];
28     }
29     for(int i=0;i<ans.size();++i){
30         printf("%05d %d ",ans[i].first,ans[i].second);
31         if(i<ans.size()-1)
32             printf("%05d\n",ans[i+1].first);
33         else
34             printf("-1\n");
35     }
36     for(int i=0;i<ans2.size();++i){
37         printf("%05d %d ",ans2[i].first,ans2[i].second);
38         if(i<ans2.size()-1)
39             printf("%05d\n",ans2[i+1].first);
40         else
41             printf("-1\n");
42     }
43     return 0;
44 }

原文地址:https://www.cnblogs.com/ldudxy/p/11963843.html

时间: 2024-10-09 16:22:39

【PAT甲级】1097 Deduplication on a Linked List (25 分)的相关文章

PAT甲级——1097 Deduplication on a Linked List (链表)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a Linked List (25 分) Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. Th

PAT Advanced 1097 Deduplication on a Linked List (25) [链表]

题目 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.

1097. Deduplication on a Linked List (25)【链表】——PAT (Advanced Level) Practise

题目信息 1097. Deduplication on a Linked List (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT 1097. Deduplication on a Linked List (25)

1097. Deduplication on a Linked List (25) 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

PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. 建立结构体节点,包括起始地址addr,下一个地址to,值value.链表数组索引为地址,接下来就是模拟链表的操作了,并且建立一个flag数组标记对应值K是否出现,若出现则flag[k]=addr,未出现则为-1,注意这里不能为0因为地址值存在为0的情况.最后的输出地址前面要补0,如果是链尾的话,-

PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; int root,n; struct No

1097. Deduplication on a Linked List (25)

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

【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)

题意: 输入两个正整数N和K(N<=40000,K<=2500),分别为学生和课程的数量.接下来输入K门课的信息,先输入每门课的ID再输入有多少学生选了这门课,接下来输入学生们的ID.最后N次询问,输入学生的ID输出该学生选了多少们课,输出所选课程的数量,按照递增序输出课程的ID. trick: 第5个数据点出现段错误,把原本以map存学生对应ID再映射vector存储该学生所选课程改成vector嵌套在map内,就没有段错误的问题出现,疑似映射过程中指针漂移??? 代码: #define H

【PAT甲级】1047 Student List for Course (25 分)

题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输出学生的名字. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s[40007];vector<string>v[2507];int main(){ ios::sync_