1097 Deduplication on a Linked List

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 (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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 1, 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

题意:

本质上是一道链表的题目,但是改变了链表的表示方法,增加了难度。

思路:

每一个结点的头指针是不会改变的,只会改变其尾指针。

Code:

#include<iostream>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<string>

using namespace std;

struct Node {
    string addr;
    int key;
    string next;
};

map<string, Node> m;
set<int> s;
queue<Node> banch;

string findNext(string start) {
    auto it = m.find(start);
    int k = abs(it->second.key);
    if (it->second.next == "-1") {
        if (s.find(k) != s.end()) {
            banch.push(it->second);
            return "-1";
        } else return start;
    }

    if (s.find(k) != s.end()) {
        banch.push(it->second);
    } else {
        return start;
    }
    return findNext(it->second.next);
}

int main() {
    string start;
    int n;
    cin >> start >> n;

    vector<Node> v1, v2;
    Node node;

    for (int i = 0; i < n; ++i) {
        int key;
        string addr, next;
        cin >> addr >> key >> next;
        node = {addr, key, next};
        m.insert({addr, node});
    }

    queue<Node> master;
    for (int i = 0; i < n; ++i) {
        auto it = m.find(start);
        node = it->second;
        s.insert(abs(node.key));
        node.next = findNext(node.next);
        master.push(node);
        start = node.next;
        if (start == "-1") break;
    }

    while(!master.empty()) {
        node = master.front();
        if (master.size() == 1) cout << node.addr << " " << node.key << " " << "-1" << endl;
        else cout << node.addr << " " << node.key << " " << node.next << endl;
        master.pop();
    }

    Node next_node;
    while(!banch.empty()) {
        node = banch.front();
        if (banch.size() == 1) {
            banch.pop();
            cout << node.addr << " " << node.key << " " << "-1" << endl;
        } else {
            banch.pop();
            cout << node.addr << " " << node.key << " " << banch.front().addr << endl;
        }
    }

    return 0;
}

  

可能是因为随着数据量的增大,会使得递归调用的层数很深,提交的时候有两组数据显示“Segmentation Fault”。



经过改进的代码消除一个“Segmentation Fault”错误,主要是做这道题的思路变了很多。

#include<iostream>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<cstdio>

using namespace std;

typedef struct Node *node;

struct Node {
    int addr;
    int key;
    int next;
    node nextnode;
    Node(int s1, int k, int s2) : addr(s1), key(k), next(s2), nextnode(NULL) {}
};

int main() {
    int n;
    int start;
    scanf("%d%d", &start, &n);
    node Head1 = (node)malloc(sizeof(struct Node));
    node Head2 = (node)malloc(sizeof(struct Node));

    node prt1, prt2, pre;
    prt1 = Head1;
    prt2 = Head2;

    map<int, node> m;
    set<int> s;
    node num, temp;

    for (int i = 0; i < n; ++i) {
        int key;
        int addr, next;
        scanf("%d%d%d", &addr, &key, &next);
        num = new Node(addr, key, next);
        m.insert({addr, num});
    }

    for (int i = 0; i < n; ++i) {
        temp = m.find(start)->second;
        prt1->nextnode = temp;
        prt1 = prt1->nextnode;
        start = prt1->next;
    }

    prt1 = Head1->nextnode;
    pre = Head1;

    while (prt1) {
        int k = abs(prt1->key);
        if (s.find(k) == s.end()) {
            prt1 = prt1->nextnode;
            pre = pre->nextnode;
            s.insert(k);
        } else {
            prt2->nextnode = prt1;
            prt2 = prt2->nextnode;
            pre->nextnode = prt1->nextnode;
            prt1 = prt1->nextnode;
        }
    }

    prt1 = Head1->nextnode;
    prt2 = Head2->nextnode;
    while (prt1) {
        if (prt1->nextnode == NULL)
            printf("%05d %d -1\n", prt1->addr, prt1->key);
        else
            printf("%05d %d %05d\n", prt1->addr, prt1->key, prt1->nextnode->addr);
        prt1 = prt1->nextnode;
    } 

    while (prt2) {
        if (prt2->nextnode == NULL)
            printf("%05d %d -1\n", prt2->addr, prt2->key);
        else
            printf("%05d %d %05d\n", prt2->addr, prt2->key, prt2->nextnode->addr);
        prt2 = prt2->nextnode;
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/ruruozhenhao/p/12609814.html

时间: 2024-10-03 06:24:18

1097 Deduplication on a Linked List的相关文章

1097. Deduplication on a Linked List (25)【链表】——PAT (Advanced Level) Practise

题目信息 1097. Deduplication on a Linked List (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT 1097. Deduplication on a Linked List (25)

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

PAT甲级——1097 Deduplication on a Linked List (链表)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 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. Th

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

PAT Advanced 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.

PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; int root,n; struct No

PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. 建立结构体节点,包括起始地址addr,下一个地址to,值value.链表数组索引为地址,接下来就是模拟链表的操作了,并且建立一个flag数组标记对应值K是否出现,若出现则flag[k]=addr,未出现则为-1,注意这里不能为0因为地址值存在为0的情况.最后的输出地址前面要补0,如果是链尾的话,-

【PAT甲级】1097 Deduplication on a Linked List (25 分)

题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int nex[100007],val[100007]; 5 vector<

pat1097. Deduplication on a Linked List (25)

1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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, f