https://www.patest.cn/contests/gplt/L2-002

模拟一个链表的去重操作

题解:别模拟了,直接用内置的list和map。关于输出的地址,直接用pair存地址和值,输出时按地址 值 下一个元素的地址 输出。

坑:之前头铁,模拟了半天,各种错误。

然后直接用list  注意删除,输出的细节。删除是常规技巧。输出时注意判掉最后一个节点,输出-1. 关于如何访问list的next:直接it++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = + ;
//double num[N], price[N], ave[N];
int nxt[N], val[N];
int nxt1[N];
map<int, int> p;
list<pair<int, int> >l,ll;
int main() {
int head, n;
cin >> head >> n;
_for(i, , n) {
int x;
cin >> x;
cin >> val[x] >> nxt[x]; } for (int p = head; p != -; p = nxt[p]) {
l.push_back(mp(p, val[p]));
} list<pair<int, int> >::iterator it;
for (it = l.begin(); it != l.end();){
if (p.count(abs(it->second))) {
ll.push_back(*it);
it = l.erase(it);
}
else p[abs(it->second)]++, it++;
}
for (it = l.begin(); it != l.end(); ) {
printf("%05d %d ", it->first, it->second);
//cout << it->first << ' ' << it->second << ' ';
if (++it != l.end())printf("%05d\n", it->first);//cout<< it->first << endl;
else cout << - << endl;
}
for (it = ll.begin(); it != ll.end(); ) {
printf("%05d %d ", it->first, it->second);
//cout << it->first << ' ' << it->second << ' ';
if (++it != ll.end())printf("%05d\n", it->first);//cout<< it->first << endl;
else cout << - << endl;
}
system("pause"); }
/*
00100 5
99999 15 87654
23854 15 00000
87654 15 -1
00000 15 99999
00100 15 23854
*/
/*
00100 -15 23854
23854 -15 00000
00000 21 99999
99999 -7 87654
87654 15 -1
00000 21 -1
*/

CCCC L2-002. 链表去重的更多相关文章

  1. 天梯 L2 链表去重

    L2-002 链表去重 (25 分) 给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另一 ...

  2. PTA L2-002 链表去重 团体程序设计天梯赛-练习集

    L2-002 链表去重(25 分)   给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另 ...

  3. 团体程序设计天梯赛-练习集L2-002. 链表去重

    L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除 ...

  4. L2-002. 链表去重

    L2-002. 链表去重 题目链接:https://www.patest.cn/contests/gplt/L2-002 这题因为结点地址只有四位数,所以可以直接开一个10000的数组模拟内存就好了. ...

  5. L2-2. 链表去重

    L2-2. 链表去重 时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的 ...

  6. C# 链表去重 List 一维 二维 分别使用 Distinct() GroupBy() 方法

    分别使用List中Distinct(),GroupBy()实现链表的去重. 1.先上效果: 一维链表中分别有元素“aa”,"bb",'aa','aa',"cc" ...

  7. L2-002 链表去重 (25 分)

    L2-002 链表去重 (25 分)   给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在 ...

  8. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  9. PAT-GPLT训练集 L2-002 链表去重

    PAT-GPLT训练集 L2-002 链表去重 题目大意为给出一个单链表,去除重复的结点,输出删除后的链表,并且把被删除的结点也以链表形式输出 思路:把这个链表直接分成两个链表,再直接输出就可以 代码 ...

  10. L2-002. 链表去重(数组模拟)

    L2-002. 链表去重 因为数值比较小,所以直接用数组来模拟 #include<cstdio> #include<cstring> #include<iostream& ...

随机推荐

  1. 随笔 -- NIO -- 相关 -- 系统概述

    .打开Selector .打开ServerSocketChannel .获取与此Channel关联的ServerSocket并绑定地址 .设置Channel为非阻塞 .将Channel注册到Selec ...

  2. ios开发之--比较两个数组里面的值是否相同

    比较两个数组里面的内容是否相同,代码如下: NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @& ...

  3. c 网络字节序和本机字节序转换

    将多字节整数类型的数据,从主机的字节顺序转化为网络字节顺序 #include <netinet/in.h> uint32_t htonl(uint32_t hostlong);uint16 ...

  4. 关于MySQL数据库

    MySQL最流行的关系型数据库管理系统 MySQL官网:http://www.mysql.com MySQL是WEB应用方面最好的RDBMS应用软件之一 RDBMS:Relational Databa ...

  5. Spring getBean 首字母大小写问题

    如果类第一个字母大写第二个小写,那么首字母小写获取bean 如果第一个和第二个字母都是大写的,那个获取bean首字母要大写

  6. 【AI】face_recognition

    1.pip install cmake 2.pip install boost 3.pip install dlib 4.pip install face_recognition

  7. Explaining Delegates in C# - Part 6 (Asynchronous Callback - Way 3)

    By now, I have shown the following usages of delegates... Callback and Multicast delegatesEventsOne ...

  8. Selenium 节点交互

    Selenium 可以驱动浏览器来执行一些操作,也就是说可以让浏览器模拟执行一些动作 常见方法:输入文字时用 send_keys() 方法,清空文字时用 clear() 方法,点击按钮时用 click ...

  9. sine曲线向前运动

    using UnityEngine; using System.Collections; public class sineWork : MonoBehaviour { float verticalS ...

  10. 第五篇:Hadoop流

    前言 Hadoop流提供了一个API,允许用户使用任何脚本语言编写Map函数或Reduce函数. 本文对此知识点进行介绍. Hadoop流的工作原理 在以前的例子中,Map和Reduce工作都是由类来 ...