天梯赛补题 —— 2020-10.11

这次天梯还凑合吧只能说,25分的题目自己基本都会相关的算法,运气好一些,35分的题就不会了

这里补一下链表去重的那个题

题目大意:

给予头指针和相关的链表数目,然后求出绝对值不同的链表,然后再求出需要删除的链表

解题思路:

  • 这题一开始想复杂了,想怎么判断下一个(甚至是下下个)是否出现,导致写的很乱
  • 最后改成了首先判断当前的指针的值是否出现,如果出现,将头指针存入删除的数组中,并继续,如果未出现,存入输出的数组中,然后继续即可。
  • 最终要判断下知否存在y值(网上代码有错的)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <stack>
#include <cmath>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 1000010, M = 200010;
const int mod = 1e9 + 7;

bool st[N];
int c[N],b[N];

struct node
{
    int sc,end;
}a[N];

int main(){
    int first, n;
    scanf("%d %d",&first,&n);
    for (int i = 1; i <= n; i++){
        int x, y , z;
        scanf("%d%d%d",&x,&y,&z);
        a[x].sc = y;
        a[x].end = z;
    }
    int x = 0, y = 0;
    while(first != -1){
        int t = fabs(a[first].sc);
        if (!st[t]){
            st[t] = true;
            b[x++] = first;
        }
        else{
            c[y ++] = first;
        }
        first = a[first].end;
    }

    for (int i = 0; i < x - 1; i ++){
        printf("%05d %d %05d\n",b[i],a[b[i]].sc,b[i + 1]);
    }
    printf("%05d %d -1\n",b[x - 1],a[b[x - 1]].sc);
    for (int i = 0; i < y - 1; i ++){
        printf("%05d %d %05d\n",c[i],a[c[i]].sc,c[i + 1]);
    }
    if (y) printf("%05d %d -1\n",c[y - 1],a[c[y - 1]].sc);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值