PAT甲级1097 Deduplication on a Linked List (25分) 解析测试点1 和测试点 2 , list 表示链表 删除元素 逆遍历

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 the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.
Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤10​5​​) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 10​4​​, and Next is the position of the next node.
Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:

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

Sample Output:

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

思路:就是原始链表里面重复的元素单独提取出来形成另一个链表,重复的定义是 绝对值一样便算作重复

使用list表示链表,结构体表示结点,然后因为结点的数据 数字并不大,直接开个大数组判断重复与否,List 逆遍历需要注意,list删除元素需要先把迭代器it保存到一个变量,迭代器it–,否则一旦erase 迭代器it,it就失效了,无法再++了;

测试点1 考察的是有多余无用的结点,可以考虑下面的测试点,看是否能通过

87654  3
99999 -7 87654
23854 -15 00000
87654 15 -1

测试点2 考察没有重复的元素

还是贴一下柳神的代码吧:https://www.liuchuo.net/archives/2118 日常膜拜

AC代码

#include <iostream>
#include <string>
using namespace std;
#include <list>
#include <unordered_map>
#include <unordered_set>
struct node
{
    int data;
    int pos;
    int next;
    node():next(-1) {}
};
int sign[100002];
int main()
{

    int start,N;
    cin>>start>>N;
    
    //使用Map保存结点的信息
    unordered_map<int,int> mapPos;
    unordered_map<int,int> mapData;
    
    for(int i=0; i<N; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        mapPos[a]=c;
        mapData[a]=b;
    }

        //构建链表
    list<node *> duo;
    for(int i=0; i<N; i++)
    {   
        //start是关键
        int data=mapData[start];
        int next=mapPos[start];
        node *p=new node();
        p->pos=start;
        p->data=data;
        p->next=next;
        duo.push_back(p);
        start=next;
        
        //测试点1 就是考察你有多余的元素,PAT之前就考察过这样的测试点
        if(p->next==-1)
            break;
    }

    
    list<node *> shao;
    //去重
    for(auto it=duo.begin(); it!=duo.end(); it++)
    {
        node *p=*it;

        int data=p->data;
        //判断
        if(data<0)
            data*=-1;
        if(sign[data]==0)
        {
            //第一次出现
            sign[data]=1;

        }
        else
        {
            //第二次出现了,需要把这个结点剔除了
            
            
            shao.push_back(p);
            //需要吧迭代器保存到另一个变量中
            auto dele=it;
            //迭代器-- 否则以erase 迭代器就失效了
            it--;
            duo.erase(dele);
        }
    }
    
    //逆遍历链表,设置每个结点的next
    for(auto it =duo.rbegin(); it!=duo.rend(); it++)
    {
        if(it==duo.rbegin())
        {
            start=(*it)->pos;
            (*it)->next=-1;
        }

        else
        {
            (*it)->next=start;
            start=(*it)->pos;
        }

    }
    for(auto it =shao.rbegin(); it!=shao.rend(); it++)
    {
        if(it==shao.rbegin())
        {
            start=(*it)->pos;
            (*it)->next=-1;
        }

        else
        {
            (*it)->next=start;
            start=(*it)->pos;
        }
    }



    for(auto it=duo.begin(); it!=duo.end(); it++)
    {
        node *p=*it;
        //cout<<p->pos<<' '<<p->data<<' '<<p->next<<endl;
        if(p->next==-1)
            printf("%05d %d %d\n",p->pos,p->data,p->next);
        else
            printf("%05d %d %05d\n",p->pos,p->data,p->next);
    }



    for(auto it=shao.begin(); it!=shao.end(); it++)
    {
        node *p=*it;
        //cout<<p->pos<<' '<<p->data<<' '<<p->next<<endl;
        if(p->next==-1)
            printf("%05d %d %d\n",p->pos,p->data,p->next);
        else
            printf("%05d %d %05d\n",p->pos,p->data,p->next);
    }
    return 0;
}

当然也可以不使用next,也就省略了把结点next 赋值成下一个结点的pos了,直接使用下一个结点的pos 输出两次就好了





#include <iostream>
#include <string>
using namespace std;
#include <list>
#include <unordered_map>
#include <unordered_set>
struct node
{
    int data;
    int pos;
    int next;
    node():next(-1) {}
};
int sign[100002];
int main()
{

    int start,N;
    cin>>start>>N;

    unordered_map<int,int> mapPos;
    unordered_map<int,int> mapData;

    for(int i=0; i<N; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        mapPos[a]=c;
        mapData[a]=b;
    }

    list<node *> duo;
    for(int i=0; i<N; i++)
    {
        int data=mapData[start];
        int next=mapPos[start];
        node *p=new node();
        p->pos=start;
        p->data=data;
        p->next=next;
        duo.push_back(p);
        start=next;
        if(p->next==-1)
            break;
    }

    list<node *> shao;
    //去重
    auto it=duo.begin();
    while(it!=duo.end())
    {
        node *p=*it;

        int data=p->data;
        //判断
        if(data<0)
            data*=-1;
        if(sign[data]==0)
        {
            //第一次出现
            sign[data]=1;
            it++;
        }
        else
        {
            //第二次出现了,需要把这个结点剔除了
            shao.push_back(p);
            auto dele=it;
            it++;
            duo.erase(dele);
        }
    }

    for(auto it=duo.begin(); it!=duo.end(); it++)
    {
        node *p=*it;
        //cout<<p->pos<<' '<<p->data<<' '<<p->next<<endl;
        if(it==duo.begin())
            printf("%05d %d ",p->pos,p->data);
        else
            printf("%05d\n%05d %d ",p->pos,p->pos,p->data);
    }
    cout<<-1<<endl;


    for(auto it=shao.begin(); it!=shao.end(); it++)
    {
        node *p=*it;
        //cout<<p->pos<<' '<<p->data<<' '<<p->next<<endl;
        if(it==shao.begin())
            printf("%05d %d ",p->pos,p->data);
        else
            printf("%05d\n%05d %d ",p->pos,p->pos,p->data);
    }
    if(shao.size()!=0)
        cout<<-1<<endl;
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值