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 (≤10​5​​) 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 10​4​​, 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[比较]的更多相关文章

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

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

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

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

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

  5. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  6. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

  7. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

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

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

随机推荐

  1. C# NameValueCollection集合 (转)

    1.NameValueCollection类集合是基于 NameObjectCollectionBase 类. 但与 NameObjectCollectionBase 不同,该类在一个键下存储多个字符 ...

  2. 使用TELNET手工操作 IMAP 查看邮件

    http://www.cnblogs.com/CrazyWill/archive/2006/08/12/474884.html IMAP 协议收信与POP收信有很大的不同,最明显的一点就是发送的每条命 ...

  3. 2016中国app年度排行榜:十大行业、25个领域、Top 500 和2017趋势预测

    本文为猎豹全球智库联合猎豹移动大数据平台libra.科技顶尖媒体36kr联合发布,如需转载必须在文章开头注明“来源:猎豹全球智库”和作者姓名,且不得更改或增删文中所有信息. 本文作者:猎豹全球智库 容 ...

  4. CodeFrist、ModelFirst、DatabaseFirst

    一.CodeFirst 使用System.Data.Entity.DbContext与System.Data.Entity.DbSet构建的数据模型,没有可视化文件但只有实体类的称为CodeFirst ...

  5. html处理富文本内容,避免XSS工具类

    import org.apache.commons.lang3.StringEscapeUtils;import org.jsoup.Jsoup;import org.jsoup.safety.Whi ...

  6. Nodejs express、html5实现拖拽上传

    一.前言 文件上传是一个比较常见的功能,传统的选择方式的上传比较麻烦,需要先点击上传按钮,然后再找到文件的路径,然后上传.给用户体验带来很大问题.html5开始支持拖拽上传的需要的api.nodejs ...

  7. 浅谈SharePoint 2013 站点模板开发

    一直以来所接触的SharePoint开发,都是Designer配合Visual Studio,前者设计页面,后者开发功能,相互合作,完成SharePoint网站开发.直到SharePoint 2013 ...

  8. opencv学习笔记——时间计算函数getTickCount()和getTickFrequency()

    cv::getTickCount()可以用来测量一段代码的运行时间,这个函数返回从上次开机算起的时钟周期数. 由于我们需要的是某个代码段运行的毫秒数,因此还需要另一个函数cv::getTickFreq ...

  9. php:// — 访问各个输入/输出流(I/O streams)

    PHP: php:// - Manual http://www.php.net/manual/zh/wrappers.php.php php:// php:// — 访问各个输入/输出流(I/O st ...

  10. arcgis api for JavaScript _加载三维图层(scene layer)

    arcgis api for JavaScript _加载三维图层(scene layer) arcgis api for JavaScript  4.x 版本增加对三维的支持. 关于三维图层(sce ...