PAT 团体天梯赛 L2-002. 链表去重

题意:中文不说了

方法:直接数组模拟链表就好了  有一个坑点  链表不一定全部包含给出的那些数据  可能只用到部分数据 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <string>
#include <map>
#include <math.h>
#include <queue>
#include <climits>
#include <stack>
using namespace std;
int Next[100005];
int ans[100005][3];
int ans2[100005][3];
int vis[100005];
int ss[100005];
int main()
{
    int fir, n;
    scanf("%d%d", &fir, &n);
    memset(Next, -1, sizeof(Next));
    for(int i = 0; i < n; i++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        ss[a] = b;
        Next[a] = c;
    }
    int cnt1 = 0, cnt2 = 0;
    memset(vis, 0, sizeof(vis));
    for(; fir != -1; fir = Next[fir]){
        int key = ss[fir];
        if(vis[abs(key)]){
            ans2[cnt2][0] = fir;
            ans2[cnt2][1] = key;
            cnt2++;
        }
        else {
            ans[cnt1][0] = fir;
            ans[cnt1][1] = key;
            vis[abs(key)] = 1;
            cnt1++;
        }
    }
    for(int i = 0; i < cnt1 - 1; i++)
        ans[i][2] = ans[i + 1][0];
    
    
    for(int i = 0; i < cnt2 - 1; i++)
        ans2[i][2] = ans2[i + 1][0];
    ans[cnt1 - 1][2] = -1;
    ans2[cnt2 - 1][2] = -1;
    for(int i = 0; i < cnt1 - 1; i++){
        printf("%05d %d %05d\n", ans[i][0], ans[i][1], ans[i][2]);
    }
    if(cnt1 )
        printf("%05d %d -1\n", ans[cnt1 - 1][0], ans[cnt1 - 1][1]);
    
    for(int i = 0; i < cnt2 - 1; i++){
        printf("%05d %d %05d\n", ans2[i][0], ans2[i][1], ans2[i][2]);
    }
    if(cnt2)
        printf("%05d %d -1\n", ans2[cnt2 - 1][0], ans2[cnt2 - 1][1]);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值