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 ;
}

//大佬的代码可以通过,我感觉我们的都一样啊 ,为什么通不过呢。。。明天再看一下,好气啊。

最新文章

  1. Ubuntu gcc缺失问题
  2. find 命令
  3. .NET框架面向对象分层的个人想理
  4. linux 下 jdk tar.gz 包安装方法
  5. Java 字符编码归纳总结
  6. 让 BAT 的 Offer 不再难拿
  7. Java基础知识强化之集合框架笔记08:Collection集合自定义对象并遍历案例(使用迭代器)
  8. MFC的杂七杂八
  9. 7. mybatis:mapper-locations: 路径放在java路径下报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
  10. [Oracle][PDB]PDB restore/recover 过程记录
  11. Console下Pause效果的函数
  12. AngularJS 杂项知识点
  13. android 解决输入法键盘遮盖布局问题
  14. JavaScript学习总结(六)——JavaScript判断数据类型总结
  15. leetCode题解之寻找一个数在有序数组中的范围Search for a Range
  16. [转]解决 Eclipse项目红感叹号
  17. STL标准库-hash
  18. 解决error C2365
  19. scrapy入门实践1
  20. java.lang.IllegalStateException: FragmentManager is already executing transactions 及 SmartTabLayout复用

热门文章

  1. &lt;转&gt;查看linux占用内存/CPU最多的进程
  2. 基于VLAN的二三层转发
  3. IOS设计模式第四篇之装饰设计模式的类别设计模式
  4. xmapp 404设置
  5. 【WEB前端开发最佳实践系列】高可读的HTML
  6. JSP中使用Spring注入的Bean时需要注意的地方
  7. JavaScript 中的 Map
  8. ubuntu 加扩展网卡遇到网卡无法识别
  9. js json转字符串
  10. X-Requested-With导致CSRF失败