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, 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 (<= 105) 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 104, 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 思路
1.map模拟一个链表dic存放输入的所有的节点,set模拟一个dupdic记录一个节点的值来判断接下来的节点是否由重复的key值,两个vector分别为去重链表和重复节点链表。
2.遍历map,根据set容器内的key值决定是将当前节点插入去重链表还是重复节点链表,并不断更新set存放的key值。
3.对两个vector新链表的节点的next地址赋予它下个节点的地址。
4.遍历两个链表并依次输出即可,
代码
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<math.h>
using namespace std;
class node
{
public:
int address;
int key;
int next;
}; map<int,node> dic;
set<int> dupdic;
vector<node> newlist;
vector<node> removed_list; int main()
{
int head,n;
while(cin >> head >> n)
{
//input
for(int i = 0;i < n;i++)
{
int adr;
cin >> adr;
dic[adr].address = adr;
cin >> dic[adr].key >> dic[adr].next;
}
//handle
while(head != -1)
{
if(dupdic.find(abs(dic[head].key)) != dupdic.end())
{
removed_list.push_back(dic[head]);
}
else
{
newlist.push_back(dic[head]);
dupdic.insert(abs(dic[head].key));
}
head = dic[head].next;
}
int lenn = newlist.size(),lenr = removed_list.size();
//output
for(int i = 1;i <= lenn;i++)
{
if(i == lenn)
{
newlist[i - 1].next = -1;
printf("%05d %d %d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
}
else
{
newlist[i - 1].next = newlist[i].address;
printf("%05d %d %05d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
}
}
for(int i = 1;i <= lenr;i++)
{
if(i == lenr)
{
removed_list[i - 1].next = -1;
printf("%05d %d %d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
}
else
{
removed_list[i - 1].next = removed_list[i].address;
printf("%05d %d %05d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
}
}
}
}

  

PAT1097:Deduplication on a Linked List的更多相关文章

  1. pat1097. Deduplication on a Linked List (25)

    1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

  2. PAT 1097 Deduplication on a Linked List[比较]

    1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...

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

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...

  4. PAT_A1097#Deduplication on a Linked List

    Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L wi ...

  5. 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 ...

  6. PAT A1097 Deduplication on a Linked List (25 分)——链表

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  7. A1097. Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

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

    http://www.patest.cn/contests/pat-a-practise/1097 Given a singly linked list L with integer keys, yo ...

  9. PAT 1097. Deduplication on a Linked List (链表)

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

随机推荐

  1. 通过Java进行网页抓取并生成plist创建代码

    抓取网页的方法: 抓取网页可以通过正则表达式也可以通过Java. 通过firefox浏览器,安装Firebug来查看网页的源代码. 首先将要抓取的部分保存到本地,步骤如下: 1.在要抓取的位置右键,选 ...

  2. Python的time(时间戳与时间字符串互相转化)

    strptime("string format")字符串如"20130512000000"格式的 输入处理函数 localtime(float a)时间戳的输入 ...

  3. PLSQL_R12 MOAC多组织的四个应用(案例)

    一.摘要 R12 Form 或者其他二次开发时,很多情况下会涉及R12 MOAC多组织开发,以下介绍了4个常见的应用,如有遗漏还请学友继续补充 1. 开发时打开Form自动弹出组织选择实现方式(增加C ...

  4. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  5. Xshell Linux 主要命令

    1.   查看当前路径 pwd 2.列出当前目录的文件 ls   列出所有文件或者文件夹 ls  *abc  列出以abc开头的所以文件 ls –l   列出所以文件及其详细详细 3.进入目录 cd  ...

  6. 传输控制协议(TCP) -- TCP状态转换图

    TCP状态转换图 在<UNIX网络编程 卷1>一书中,作者给出了TCP状态转换图(如下).本文也将围绕此图进行阐释. 注:上图红框表示比较特殊的地方. TCP状态转换两条主线 图2-4中的 ...

  7. 关于MySQL主从复制中UUID的警告信息

    日期: 2014年5月23日 博客: 铁锚 最近在查看MariaDB主从复制服务器 Master 的错误日志时看到很多条警告信息,都是提示 UUID()函数不安全,可能 Slave 产生的值和 Mas ...

  8. ROS探索总结(十四)——move_base(路径规划)

    在上一篇的博客中,我们一起学习了ROS定位于导航的总体框架,这一篇我们主要研究其中最重要的move_base包. 在总体框架图中可以看到,move_base提供了ROS导航的配置.运行.交互接口,它主 ...

  9. GNU中的处理目标文件的若干工具

    AR 创建静态库,插入.删除.列出和提取成员: SRING 列出目标文件中的字符串: SIRIP 从目标文件中删除符号表信息: NM 列出目标文件符号表中定义的符号: SIZE 列出目标文件中节的名字 ...

  10. DB2常用命令小结

    PS:执行命令前需要进入DB2的账户下:su db2inst1 修改密码:更改相应的操作系统密码即可,windows上可以更改db2admin的密码,linux上更改db2inst1的密码即可,db2 ...