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 Node
{
    int id,val,nx;
}tmp[maxn],s[maxn];

int in[maxn];
int flag[maxn];
int idx[maxn];
int tot=0;
vector<int>g[maxn];

int main()
{
    memset(in,0,sizeof in);
    memset(flag,0,sizeof flag);
    scanf("%d%d",&root,&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&tmp[i].id,&tmp[i].val,&tmp[i].nx);
        in[tmp[i].id]=i;
    }

    int p=root,sz=0;
    while(1)
    {
        if(p==-1) break;
        s[sz++]=tmp[in[p]];
        p=tmp[in[p]].nx;
    }

    for(int i=0;i<sz;i++)
    {
        if(flag[abs(s[i].val)]==0) g[0].push_back(i);
        else g[1].push_back(i);
        flag[abs(s[i].val)]++;
    }

    for(int i=0;i<=1;i++)
    {
        for(int j=0;j<g[i].size();j++)
        {
            if(j==g[i].size()-1)
                printf("%05d %d %d\n",s[g[i][j]].id,s[g[i][j]].val,-1);
            else
                printf("%05d %d %05d\n",s[g[i][j]].id,s[g[i][j]].val,s[g[i][j+1]].id);
        }
    }
    return 0;
}
时间: 2024-12-14 18:14:27

PAT (Advanced Level) 1097. Deduplication on a Linked List (25)的相关文章

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

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

【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<

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 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)

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 (Advanced Level) 1079. Total Sales of Supply Chain (25)

树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT (Advanced Level) 1006. Sign In and Sign Out (25)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; struct X { string name; int a,b; }s[10000]; int n,hh,mm,ss; bool cmp1(const X&a,co

PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=100000+10; vector<int>g[