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 ...
随机推荐
- springboot---->集成mybatis开发(二)
这里面我们介绍一下springboot集成mybatis完成一对多数据和一对一数据的功能.任何一个人离开你 都并非突然做的决定 人心是慢慢变冷 树叶是渐渐变黄 故事是缓缓写到结局 而爱是因为失望太多 ...
- SQL数据库对象名无效的解决方法
对象名 'dbo.xxxx' 无效. 最后找到如下方法解决:原因是必须把所有以前的所有者改为DBO就不会出问题了. 执行下面语句,更改所有表的所有者为DBO exec sp_msforeachtabl ...
- Python OS 文件/目录方法
Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...
- smali-2.2.4.jar & baksmali-2.2.4.jar
https://bitbucket.org/JesusFreke/smali/downloads/
- Window 命令行神器:cmder
http://cmder.net/ https://github.com/cmderdev/cmder/releases/ 官网下载地址 http://www.360doc.com/content ...
- 怎么使用jstack精确找到异常代码
1.代码demo //一个CPU密集型线程的demo: package chapter1; public class FindJavaThreadInTaskManager { public stat ...
- 更新jenkins插件,报错 Perhaps you need to run your container with "-Djava.awt.headless=true"?
Configuring the Java environment variables vi ~/.bash_profile 在最后一行加入: export JAVA_OPTS=-Djava.awt.h ...
- dhroid - ioc基础(@Inject*)
1 ioc即控制反转.控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心.控制反转还有一 ...
- 富文本编辑器TinyMCE
最近项目中用到了javascript富文本编辑器,从网上找开源控件,发现很多可选,参考下面文章,列出了很多可用的插件http://www.cnblogs.com/ywqu/archive/2009/1 ...
- vue之修饰符
修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 .你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: <!-- ...