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 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
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
struct node{
int id,data,next;
};
int main(){
int s,n,b=-1,index,check[10000]={0},t1=-1,last=-1,tag=0;
cin>>s>>n;
node list[100000];
for(int i=0;i<n;i++){
node nod;
cin>>nod.id>>nod.data>>nod.next;
list[nod.id]=nod;
}
index=s;
vector<node> single,del;
while(index!=-1){
if(check[abs(list[index].data)]==0)
single.push_back(list[index]);
else
del.push_back(list[index]);
check[abs(list[index].data)]=1;
index=list[index].next;
}
for(int i=0;i<single.size();i++){
if(i!=single.size()-1)
printf("%05d %d %05d\n",single[i].id,single[i].data,single[i+1].id);
else
printf("%05d %d %d\n",single[i].id,single[i].data,-1);
}
for(int i=0;i<del.size();i++){
if(i!=del.size()-1)
printf("%05d %d %05d\n",del[i].id,del[i].data,del[i+1].id);
else
printf("%05d %d %d\n",del[i].id,del[i].data,-1);
}
return 0;
}
PAT 1097. Deduplication on a Linked List (链表)的更多相关文章
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- 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 ...
- 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 ...
- 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 duplica ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 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 ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 1097 Deduplication on a Linked List
题意: 给出一个链表,删除绝对值相同的结点,对于每个绝对值为K的结点,只保留第一次出现的那个.把被移除的结点组成一个新链表,输出删除去重后的链表和新链表. 思路:考察链表的“删除”操作,不难. 代码: ...
随机推荐
- MySQL5.6 GTID方式,配置主从
迁移数据到从库 数据导出: mysqldump -uroot -p111111 -h127. -P3306 -q --single-transaction -R -E --triggers --def ...
- 浅析js的函数的按值传递参数
js的函数传参的方式是按值传递,正常情况下,改变函数参数的值,并不会对函数外部的变量造成影响.例如: 'use strict';var list = [1, 2, 3]; list.forEach(f ...
- C - Gr-idian MST
Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement On an xy plane, in an ...
- android5.1 Recovery添加从U盘升级功能【转】
本文转载自:http://blog.csdn.net/tfslovexizi/article/details/73835594 之前看到过一个人写了4.4上添加U盘升级功能的博客http://blog ...
- SQLServer添加链接服务器
右键,添加链接服务器 在安全里面输入用户名和密码 添加成功之后的使用方法 select * from [192.168.1.63,3326].[数据库].[dbo].[表]
- [JSOI 2012] 玄武密码
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4327 [算法] AC自动机[代码] #include<bits/stdc++. ...
- 65. ExtJs获取文本框中值的几种方式
转自:https://blog.csdn.net/qiu512300471/article/details/17415675/ 1.Html文本框 如:<input type=" ...
- CSS小代码汇总整理
/**实现斑马线的表格*/table.flexme tbody tr:nth-child(2n){background-color:#D6E7FC;} /*返回偶数序的子元素*/table.flexm ...
- 入门activiti-------1简单运行
1.下载原料 2.放置位置 3.运行 4.成功页面和测试数据
- es6 import 与 export
1.export 命令 export 命令用于规定模块的对外接口. 一个模块就是一个独立的文件.该文件内部所有的变量,外部无法获取.要想外部能够读取模块内部的某个变量,就必须使用 export 关键字 ...