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 ...
随机推荐
- <转>查看linux占用内存/CPU最多的进程
转自 http://beginman.cn/page26/ 查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 10 或者top (然后按下M,注意大写) 查使用 ...
- Android设计和开发系列第二篇:Action Bar(Develop—Training)
Adding the Action Bar GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.1 or higher YOU SHOULD AL ...
- 【Linux】 crontab 实现每秒执行
linux crontab 命令,最小的执行时间是一分钟, 如果要在小于一分钟执行.就要换个方法来实现 1 crontab 的延时: 原理:通过延时方法 sleep N 来实现每N秒执行. cr ...
- c++ 类前置声明【转】
[转自 here] 在编写C++程序的时候,偶尔需要用到前置声明(Forward declaration).下面的程序中,带注释的那行就是类B的前置说明.这是必须的,因为类A中用到了类B,而类B的声明 ...
- mFC 橡皮线
一般都用GDI实现: void CXiangpijinView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message ...
- Android Studio 删除 Module
1.选中Module右击,选择 Open Module Settings,打开Project Structure 窗空.(或者选中Module,按F4打开Project Structure窗口) 2. ...
- Eclipse的控制台console经常闪现
Eclipse的控制台console有时候经常闪现! 让它不经常的调出来,可以按下面的操作去掉它: windows -> preferences -> run/debug ...
- aws.s3的 upload 和putObject有什么区别
相同点:上传或新增一个object : <template> <div class="page"> <!-- 参考:https://blog.csdn ...
- 根据json对象的值替换json数组里的值
功能: var fruitArry=[{name:'durian'},{name:'peach'},{name:'banana'},{name:'pitaya'},{name:'apple'},{na ...
- [Offer收割]编程练习赛15 B.分数调查[加权并查集]
#1515 : 分数调查 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi的学校总共有N名学生,编号1-N.学校刚刚进行了一场全校的古诗文水平测验. 学校没有公布测 ...