当前位置:网站首页>Pat 1097 duplication on a linked list (25 points)
Pat 1097 duplication on a linked list (25 points)
2022-07-06 12:08:00 【Python ml】
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct node{
int address,val;
};
int Data[100005],Next[100005];
int main() {
int first,n,temp;
cin>>first>>n;
vector<node>a(n),a1,a2;
for(int i=0;i<n;i++){
scanf("%d",&temp);
scanf("%d %d",&Data[temp],&Next[temp]);
}
int i=0;
while(first!=-1){
a[i++]={
first,Data[first]};
first=Next[first];
}
set<int>s;
for(int i=0;i<n;i++){
if(s.find(abs(a[i].val))==s.end()){
s.insert(abs(a[i].val));
a1.push_back({
a[i].address,a[i].val});
}else{
a2.push_back({
a[i].address,a[i].val});
}
}
for(int i=0;i<a1.size();i++){
printf("%05d %d ",a1[i].address,a1[i].val);
if(i!=a1.size()-1) printf("%05d\n",a1[i+1].address);
else cout<<-1<<endl;
}
for(int i=0;i<a2.size();i++){
printf("%05d %d ",a2[i].address,a2[i].val);
if(i!=a2.size()-1) printf("%05d\n",a2[i+1].address);
else cout<<-1<<endl;
}
system("pause");
return 0;
}

#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = 100000;
struct NODE {
int address, key, next, num = 2 * maxn;
}node[maxn];
bool exist[maxn];
int cmp1(NODE a, NODE b){
return a.num < b.num;
}
int main() {
int begin, n, cnt1 = 0, cnt2 = 0, a;
scanf("%d%d", &begin, &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a);
scanf("%d%d", &node[a].key, &node[a].next);
node[a].address = a;
}
for(int i = begin; i != -1; i = node[i].next) {
if(exist[abs(node[i].key)] == false) {
exist[abs(node[i].key)] = true;
node[i].num = cnt1;
cnt1++;
}
else {
node[i].num = maxn + cnt2;
cnt2++;
}
}
sort(node, node + maxn, cmp1);
int cnt = cnt1 + cnt2;
for(int i = 0; i < cnt; i++) {
if(i != cnt1 - 1 && i != cnt - 1) {
printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+1].address);
} else {
printf("%05d %d -1\n", node[i].address, node[i].key);
}
}
return 0;
}

边栏推荐
- Use of lists
- Inline detailed explanation [C language]
- Analysis of charging architecture of glory magic 3pro
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
- Apprentissage automatique - - régression linéaire (sklearn)
- 树莓派 轻触开关 按键使用
- Pytorch实现简单线性回归Demo
- Some concepts often asked in database interview
- Arm pc=pc+8 is the most understandable explanation
- C语言回调函数【C语言】
猜你喜欢
![[esp32 learning-2] esp32 address mapping](/img/ee/c4aa0f7aed7543bb6807d7fd852c88.png)
[esp32 learning-2] esp32 address mapping

Detailed explanation of 5g working principle (explanation & illustration)

MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解

RT thread API reference manual

MySQL数据库面试题

ES6 grammar summary -- Part 2 (advanced part es6~es11)

Arm pc=pc+8 is the most understandable explanation

电商数据分析--用户行为分析

MySQL realizes read-write separation

I2C bus timing explanation
随机推荐
E-commerce data analysis -- User Behavior Analysis
Apprentissage automatique - - régression linéaire (sklearn)
RT-Thread 线程的时间片轮询调度
AMBA、AHB、APB、AXI的理解
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
Reno7 60W超级闪充充电架构
VIM command line notes
Selective sorting and bubble sorting [C language]
I2C总线时序详解
Arm pc=pc+8 is the most understandable explanation
Reading notes of difficult career creation
The first simple case of GNN: Cora classification
选择法排序与冒泡法排序【C语言】
E-commerce data analysis -- salary prediction (linear regression)
互聯網協議詳解
共用体(union)详解【C语言】
Arduino get random number
RT-Thread API参考手册
Detailed explanation of Union [C language]
Reno7 60W super flash charging architecture