L2_002链表去重(未解决)

2018-03-24  本文已影响0人  我好菜啊_



这个人用的方法跟我一摸一样但他说他ac了
https://www.cnblogs.com/yinzm/p/5499231.html


#include <iostream>
#include <iomanip>
#include <cmath>
#define N 100000
using namespace std;
struct node{
    int key;
    int next;
};
node arr[N];
int have[10001]={0};
int main()
{
    int head,delete_head,delete_last;
    int n;
    cin>>head>>n;
    int flag=0;
    for(int i=1;i<=n;++i){
        int xadd,xkey,xnext;
        cin>>xadd>>xkey>>xnext;
        node no;
        no.key=xkey;no.next=xnext;
        arr[xadd]=no;
    }
    int a=abs(arr[head].key);
    have[a]=1;
    int pos=head;
    int tp=arr[pos].next;
    while(tp!=-1){
        //int tp=arr[pos].next;
        //注意审题是绝对值!!
        int a=abs(arr[tp].key);
        if(have[a]){
            if(!flag){
                delete_head=tp;
                delete_last=tp;
                flag=1;
            }else{
                arr[delete_last].next=tp;
                delete_last=tp;
            }
            arr[pos].next=arr[tp].next;
        }else{
            //这里也是绝对值之前忘改了
            have[a]=1;
            pos=tp;
        }
        tp=arr[pos].next;
        //pos=tp;//此处tp可能已经被删掉了不能这样写!!
        //pos=arr[pos].next;
        //tp=arr[tp].next;注意这部多余啦!!
    }
    arr[delete_last].next=-1;
    pos=head;
    int flag2=0;
    while(pos!=-1){
        if(flag2)
            cout<<endl;
        flag2=1;
        cout<<setw(5)<<setfill('0')<<pos<<" "<<arr[pos].key<<" ";
        if(arr[pos].next!=-1)
        {
            cout<<setw(5)<<setfill('0')<<arr[pos].next;
        }
        else
            cout<<-1;
        pos=arr[pos].next;
    }
    pos=delete_head;
    while(pos!=-1)
    {
        cout<<endl;
        cout<<setw(5)<<setfill('0')<<pos<<" "<<arr[pos].key<<" ";
        if(arr[pos].next!=-1)
        {
            cout<<setw(5)<<setfill('0')<<arr[pos].next;
        }
        else
            cout<<-1;
        pos=arr[pos].next;
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#define N 100000
#define K 10001
using namespace std;
struct node
{
    int addr;
    int key;
    int next;
    int num;
};
bool comp(node &n1, node &n2)
{
    return n1.num < n2.num;
}
node nums[N];
int have[K] = { 0 };
int main()
{
    int start, n;
    scanf("%d%d", &start, &n);
    for (int i = 0; i < N; ++i) {
        nums[i].num = 2 * N;
        //注意是全部都要初始化成2N而不只是读到的数据
    }
    for (int i = 0; i < n; ++i) {
        int taddr;
        scanf("%d", &taddr);
        nums[taddr].addr = taddr;
        scanf("%d", &nums[taddr].key);
        scanf("%d", &nums[taddr].next);
        //nums[taddr].num = 2 * N;
    }
    int cnt1 = 0, cnt2 = 0;
    int t = start;
    while (t != -1) {
        if (have[abs(nums[t].key)]) {
            nums[t].num = N + cnt2;
            ++cnt2;
        }
        else {
            have[abs(nums[t].key)] = 1;
            nums[t].num = cnt1;
            ++cnt1;
        }
        t = nums[t].next;
    }
    sort(nums, nums + N,comp);
    int cnt = cnt1 + cnt2;
    for (int i = 0; i < cnt; ++i)
    {
        if (i)
            cout << endl;
        printf("%05d %d ", nums[i].addr, nums[i].key);
        if (i == cnt - 1 || i == cnt1 - 1)
            cout << -1;
        else
            printf("%05d", nums[i+1].addr);
    }
    system("pause");
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读