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. 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz

    10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...

  2. 随笔 -- IO -- Socket/ServerSocket -- Echo(BIO)实例

    随笔 -- IO -- Socket/ServerSocket -- 系统概述 Java中提供的专门的网络开发程序包------java.net Java的网络编程提供的两种通信协议:TCP和UDP ...

  3. mongodb int字段的一个小坑

    在使用 php mongodb 搜索时,如果字段类型用 int,则使用 php 搜索时一定要把数值转换成整型来搜索,用字符串类型的数字搜索是没有结果的!!!! $condition = ['membe ...

  4. 【安全开发】C/C++安全编码规范

    C本质上是不安全的编程语言.例如如果不谨慎使用的话,其大多数标准的字符串库函数有可能被用来进行缓冲区攻击或者格式字符串攻击.但是,由于其灵活性.快速和相对容易掌握,它是一个广泛使用的编程语言.下面是针 ...

  5. Create maintenance backup plan in SQL Server 2008 R2 using the wizard

    You will need to identify how you want your maintenance plan to be setup. In this example the mainte ...

  6. OSG添加回调更新

    class CB : public osg::NodeCallback { virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) ...

  7. zabbix的源码安装

    前提:安装好lnmp环境,参考: 搭建LNMP环境   下载软件包 1. 下载并解压安装包 cd /usr/local/src wget https://ncu.dl.sourceforge.net/ ...

  8. PowerShell的初步学习

    今天要重新学习一钟语法,由于工作中项目的需要,不得不说学习新的语言是必不可少的.          Windows PowerShell 是一种命令行外科程序和脚本环境,使命令行用户和脚本编写者可以利 ...

  9. 在iOS中使用icon font

    博文转载至 http://www.cocoachina.com/industry/20131111/7327.html 在开发阿里数据iOS版客户端的时候,由于项目进度很紧,项目里的所有图标都是用最平 ...

  10. leetCode练习题

    1.求二叉树的最小深度: public class Solution { public int run(TreeNode root) { if(root==null) return 0; int l ...