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. 网易 2016 实习研发project师 3道 编程题

    1 比較重量 给定两颗钻石的编号g1,g2,编号从1開始.同一时候给定关系数组vector,当中元素为一些二元组.第一个元素为一次比較中较重的钻石的编号,第二个元素为较轻的钻石的编号.最后给定之前的比 ...

  2. cocos2d-x 弹出对话框

    登陆界面有一点注册按钮,点击之后弹出一个框,让用户输入一些信息; 在网上找的一些方法都是派生一个类,然后自己实现; 我觉得太麻烦了. 我使用分层的方式来显示, 启动界面就一个,背景图片加几个按钮:登陆 ...

  3. 如何关闭Struts2的webconsole.html

    出于安全目的,在禁用了devMode之后,仍然不希望其他人员看到webconsole.html页面,则可以直接删除webconsole.html 的源文件, 它的位置存在于: 我们手工删除 strut ...

  4. [Maven]Maven中的一些基本概念

    Pom文件中的groupId.artifactId.version和name,这三个元素定义了一个项目的基本的坐标,在Maven世界中,任何的jar.pom或者war都是基于这些基本的坐标惊醒区分. ...

  5. hive 安装记录

    http://www.cnblogs.com/linjiqin/archive/2013/03/04/2942402.html

  6. Oracle 常用函数备查

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...

  7. [Ubuntu] apt 添加第三方库

    1. 方法一:直接在 /etc/apt/sources.list 添加第三方库. $ sudo vi /etc/apt/sources.list 在其中添加: deb http://archive.s ...

  8. N76E003之定时器3

    定时器3是一个16位自动重装载,向上计数定时器.用户可以通过配置T3PS[2:0] (T3CON[2:0])选择预分频,并写入重载值到R3H 和R3L寄存器来决定它的溢出速率.用户可以设置TR3 (T ...

  9. iOS 9: UIStackView入门

    本文转自http://www.cocoachina.com/ios/20150623/12233.html 本文由CocoaChina译者candeladiao翻译,欢迎参加我们的翻译活动.原文:iO ...

  10. 【.netcore学习】.netcore添加到 supervisor 守护进程自启动报错

    配置 supervisor [program:HelloWebApp] command=dotnet run directory=/home/python/dotnet/myweb/mywebapi ...