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 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.这些数是整合成结构体的形式,还是存成单独的数组?2.怎么区分两个链表?
代码来自:https://www.liuchuo.net/archives/2118
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = ;
struct NODE {
int address, key, next, num = * maxn;
}node[maxn];
bool exist[maxn];
int cmp1(NODE a, NODE b){
return a.num < b.num;
}
int main() {
int begin, n, cnt1 = , cnt2 = , a;
scanf("%d%d", &begin, &n);
for(int i = ; i < n; i++) {
scanf("%d", &a);
scanf("%d%d", &node[a].key, &node[a].next);
node[a].address = a;//将所有的点保存。
}
for(int i = begin; i != -; i = node[i].next) {
if(exist[abs(node[i].key)] == false) {
exist[abs(node[i].key)] = true;
node[i].num = cnt1;
cnt1++;//通过这个来计数,厉害了啦!解决了链表的顺序问题。
}
else {
node[i].num = maxn + cnt2;
cnt2++;//对重复的计数。
}
}
sort(node, node + maxn, cmp1);//使用sort排序之后,就没有地址下下标的区分了。
//这样排序之后保证了重复的排在后面,同时也保证了链表的顺序。
int cnt = cnt1 + cnt2;
for(int i = ; i < cnt; i++) {
if(i != cnt1 - && i != cnt - ) {//这两个涵盖的情况是正常链最后一个,和非正常链最后一个
printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+].address);
} else {
printf("%05d %d -1\n", node[i].address, node[i].key);
}
}
return ;
}
//感觉超厉害,我实在是想不出这种办法,学习了!
https://blog.csdn.net/realxuejin/article/details/49362519
这位大佬使用向量存储,很受启发,今天晚上写一下。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include<algorithm>
using namespace std;
struct Node{
int addr,num,next;
}node[];
int flag[];
int main()
{
int head,n;
scanf("%d %d",&head,&n);
int a;
vector<Node> v1,v2;//分别存储正常和重复的两个链表。
for(int i=;i<n;i++){
scanf("%d",&a);
scanf("%d %d",&node[a].num,&node[a].next);
node[a].addr=a;//这里一定要将所有的节点都保存,然后再进行操作,因为直接操作可能有无用的点扰乱。
}
if(node[head].addr==-){
cout<<'\n';return ;
}
for(int i=head;i!=-;i=node[i].next){
if(flag[abs(node[i].num)]==){
flag[abs(node[i].num)]=;
v1.push_back(node[i]);//
}else
v2.push_back(node[i]);
}
for(int i=;i<v1.size();i++){
if(i!=v1.size()-)
printf("%05d %d %05d\n",v1[i].addr,v1[i].num,v1[i+].addr);
else
printf("%05d %d -1\n",v1[i].addr,v1[i].num);
}
if(v2.size()==)return ;
for(int i=;i<v2.size();i++){
if(i!=v2.size()-)
printf("%05d %d %05d\n",v2[i].addr,v2[i].num,v2[i+].addr);
else
printf("%05d %d -1\n",v2[i].addr,v2[i].num);
} return ;
}
/**
-1 0 1 2
1 2 3
3 1 -1
**/
//这是我写的代码,测试点3通不过,
#include <iostream>
#include <vector>
#include <cmath>
#include<stdio.h>
using namespace std; struct node{
int addr;
int key;
int next;
}; int main(int argc, char** argv) {
int root, n, i;
scanf("%d%d",&root, &n);
vector<node> list(); int addr, key, next;
for(i=; i<n; i++){
scanf("%d %d %d",&addr,&key,&next);
list[addr].key = key;
list[addr].next = next;
list[addr].addr = addr;
} if(list[root].addr == -){
cout<<endl;
return ;
} vector<int> flag(,-);
vector<node> cut; //存放因为重复而被删掉的节点
vector<node> vec;
int index = root;
while( index != -){
key = abs(list[index].key);
if(flag[key] == -){
flag[key] = ;
vec.push_back(list[index]);
}else{
cut.push_back(list[index]);
}
index = list[index].next;
} node Node;
for(i=; i<vec.size()-; i++){
Node = vec[i];
printf("%05d %d %05d\n", Node.addr, Node.key, vec[i+].addr );
}
Node = vec[vec.size()-];
printf("%05d %d -1\n", Node.addr, Node.key ); //没有重复的情况下
if(cut.size() == )
return ;
for(i=; i<cut.size()-; i++){
Node = cut[i];
printf("%05d %d %05d\n", Node.addr, Node.key, cut[i+].addr );
}
Node = cut[cut.size()-];
printf("%05d %d -1\n", Node.addr, Node.key ); return ;
}
//大佬的代码可以通过,我感觉我们的都一样啊 ,为什么通不过呢。。。明天再看一下,好气啊。
PAT 1097 Deduplication on a Linked List[比较]的更多相关文章
- 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 ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- 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 (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- 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 ...
- 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 ...
随机推荐
- SQL Server设置登录验证模式
我们在安装SQL Server的时候可以设置“混合验证模式”,既可以使用windows身份验证登录,也可以使用SQL Server身份验证登录. 如果我们在安装的时候并未设置"混合验证模式& ...
- Promise最佳实践(转)
本文作者:IMWeb dekuchen 原文出处:IMWeb社区 未经同意,禁止转载 有关Promise的几个问题 基础概念 一:什么是Promise 国内比较流行的看法: 阮一峰: Promise ...
- Hibernate 查询sql结果行数的几种方法
一.前言 这个东西,难度几乎没有,就是繁琐. 一条简单的select count(*) from table_name 都能有多种书写方式. 总是忘,这里记录下. 一 .通过Criteria 查询 C ...
- Centos 升级 python
昨天把redmine的测试环境给搞Over了,想了下,干脆直接把环境给整成docker化的,配置环境的时候,安装docker-compose需要python2.7支持. CentOS 6 系统默认 P ...
- C# List<T> 泛型
1.简介 所属命名空间:System.Collections.Generic List<T>类是 ArrayList 类的泛型等效类.该类使用大小可按需动态增加的数组实现 IList< ...
- 23种设计模式之备忘录模式(Memento)
备忘录模式确保在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态.备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定 ...
- 自动释放池autoreleasepool
自动释放池是NSAutoreleasePool的实例,其中包含了收到autorelease消息的对象.当一个自动释放池自身被销毁(dealloc)时,它会给池中每一个对象发送一个release消息(如 ...
- mysql如何使用索引index提升查询效率?
https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html Indexes are used to find rows with specif ...
- mysql导入存储过程
查询数据库存储过程 select `name` from mysql.proc where db = 'databaseName' and `type` = 'PROCEDURE'; mariadb操 ...
- POJ-2346 Lucky tickets(线性DP)
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...