2019天梯赛训练1 7-10 链表去重 (25 分)

给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所有被删除的结点须被保存在另一个链表上。例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15。

输入格式:

输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤10​5​​,为结点总数)。一个结点的地址是非负的 5 位整数,空地址 NULL 用 -1 来表示。

随后 N 行,每行按以下格式描述一个结点:

地址 键值 下一个结点

其中地址是该结点的地址,键值是绝对值不超过10​4​​的整数,下一个结点是下个结点的地址。

输出格式:

首先输出去重后的链表,然后输出被删除的链表。每个结点占一行,按输入的格式输出。

输入样例:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

输出样例:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

此题我一开始是用链表写的,得了22分。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=1e6+5;
int loc[maxn];
int Key[maxn];
int st,n;
struct node
{
    int pre;
    int pos;
    int key;
    int next;
};
node a[maxn];
int dst=-1;
int main()
{
    int cnt=0;
    memset (loc,-1,sizeof(loc));
    memset (Key,0,sizeof(Key));
    scanf("%d%d",&st,&n);
    for (int i=0;i<n;i++)
    {
        scanf("%d%d%d",&a[i].pos,&a[i].key,&a[i].next);
        loc[a[i].pos]=i;
    }
    for (int i=0;i<n;i++)
    {
        if(a[i].next==-1) continue;
        int t=loc[a[i].next];
        a[t].pre=a[i].pos;
    }
    if(loc[st]==-1)
        st=-1;
    int tst=st, tdst=dst;
    while (tst!=-1)
    {
        int t=loc[tst];
        int temp=a[t].next;
        if(Key[abs(a[t].key)]==1)
        {
           int pre=a[t].pre;
           int te=loc[pre];
           a[te].next=a[t].next;
           int tt=loc[a[t].next];
           if(a[t].next!=-1)
                a[tt].pre=pre;
           if(dst==-1)
           {
               dst=a[t].pos;
               tdst=dst;
               a[t].next=-1;
           }
           else
           {
               int t1=loc[tdst];
               a[t1].next=a[t].pos;
               a[t].pre=a[t1].pos;
               tdst=a[t].pos;
               a[t].next=-1;
           }
        }
        else
        {
            Key[abs(a[t].key)]=1;
        }
        tst=temp;
    }
    tst=st;
    while (tst!=-1)
    {
        int t=loc[tst];
        printf("%05d %d ",a[t].pos,a[t].key);
        if(a[t].next==-1)
            printf("-1\n");
        else
            printf("%05d\n",a[t].next);
        tst=a[t].next;
    }
    tdst=dst;
    while (tdst!=-1)
    {
        int t=loc[tdst];
        printf("%05d %d ",a[t].pos,a[t].key);
        if(a[t].next==-1)
            printf("-1\n");
        else
            printf("%05d\n",a[t].next);
        tdst=a[t].next;
    }
    return 0;
}

之后又学习了下别人的处理方式:

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn=1e6+5;
struct node
{
    int next;
    int key;
};
node a[maxn];
int vis[maxn];
int st,n;
int a1[maxn],cnt1=0;
int a2[maxn],cnt2=0;
int main()
{
    memset (vis,0,sizeof(vis));
    scanf("%d%d",&st,&n);
    for (int i=0;i<n;i++)
    {
        int ad;
        scanf("%d",&ad);
        scanf("%d%d",&a[ad].key,&a[ad].next);
    }
    for (int i=st;i!=-1;i=a[i].next)
    {
        if(vis[abs(a[i].key)])
        {
             a2[cnt2++]=i;
        }
        else
        {
            vis[abs(a[i].key)]=1;
            a1[cnt1++]=i;
        }
    }
    printf("%05d %d ",a1[0],a[a1[0]].key);
    for (int i=1;i<cnt1;i++)
    {
        printf("%05d\n",a1[i]);
        printf("%05d %d ",a1[i],a[a1[i]].key);
    }
    printf("-1\n");
    if(cnt2)
    {
        printf("%05d %d ",a2[0],a[a2[0]].key);
        for (int i=1;i<cnt2;i++)
        {
            printf("%05d\n",a2[i]);
            printf("%05d %d ",a2[i],a[a2[i]].key);
        }
        printf("-1\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/86664894