• PAT L2-002 链表去重


    题目链接:PAT L2-002 链表去重

    题意:

    让你将一个链表按绝对值去重

    题解;

    模拟一下就行,注意输出格式,地址要用%05d。

     1 #include<bits/stdc++.h>
     2 #define F(i,a,b) for(int i=a;i<=b;++i)
     3 using namespace std;
     4 
     5 const int N=1e5+7;
     6 struct dt
     7 {
     8     int data,nxt;
     9 }a[N*10];
    10 int hsh[N];
    11 int main()
    12 {
    13     int head,n;
    14     scanf("%d%d",&head,&n);
    15     F(i,1,n)
    16     {
    17         int hd,data,nxt;
    18         scanf("%d%d%d",&hd,&data,&nxt);
    19         a[hd].data=data,a[hd].nxt=nxt;
    20     }
    21     int second_head=-1,now=-1,pre=-1;
    22     for(int i=head;~i;i=a[i].nxt)
    23     {
    24         if(hsh[abs(a[i].data)])
    25         {
    26             if(second_head==-1)
    27             {
    28                 second_head=i;
    29                 now=i;
    30             }else
    31             {
    32                 a[now].nxt=i;
    33                 now=i;
    34             }
    35         }else
    36         {
    37             hsh[abs(a[i].data)]=1;
    38             if(pre!=-1)
    39             {
    40                 a[pre].nxt=i;
    41             }
    42             pre=i;
    43         }
    44     }
    45     a[pre].nxt=-1;
    46     a[now].nxt=-1;
    47     for(int i=head;~i;i=a[i].nxt)
    48     {
    49         printf("%05d %d ",i,a[i].data);
    50         if(a[i].nxt==-1)puts("-1");
    51         else printf("%05d
    ",a[i].nxt);
    52     }
    53     for(int i=second_head;~i;i=a[i].nxt)
    54     {
    55         printf("%05d %d ",i,a[i].data);
    56         if(a[i].nxt==-1)puts("-1");
    57         else printf("%05d
    ",a[i].nxt);
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    Android 引用资源
    Android res目录结构
    Android 目录结构
    ubuntu 14.04 (desktop amd 64) 查看配置参数
    ros service
    install ros-indigo-map-server
    python 单例
    查看指定目录空间占用
    shell 设置超时时间
    nohup 不生成 nohup.out的方法
  • 原文地址:https://www.cnblogs.com/bin-gege/p/6707112.html
Copyright © 2020-2023  润新知