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. Java -- IO -- 目录

    操作文件的类 -- -- File File类的基本介绍 使用File类操作文件 范例 -- -- 列出指定目录的全部内容 RandomAccessFile类 使用RandomAccessFile类写 ...

  2. 架设SVN服务器

    服务器端:windows 2003 + subversion 下载地址:http://subversion.tigris.org/servlets/ProjectDocumentList?folder ...

  3. 【Nodejs】npm cnpm 淘宝镜像

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...

  4. 深入理解磁盘文件系统之inode

    一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...

  5. IIS中采用ISAPI-Rewrite防盗链

    本规则支持白名单排除式防盗链,搜索引擎友好(不屏蔽),被盗链后的错误提示转向,支持各种文件类型,经作者亲验真的能用,第一时间在itmop.com原创发表,请继续往下阅读. 近来小站遇到了盗链问题,至使 ...

  6. Linux命令之乐--iconv

    用法: 实际应用: 批量转换文件编码: [root@wls12c PCK]$ for tfile in `ls -l|awk '{print $9}'`;do echo "iconv -f ...

  7. oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame

    import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...

  8. mosquitto 参数配置

    mosquitto 参数配置 1.retry_interval  当QoS为1或2的消息已经被发送后,mosquitto在一段时间内仍未接收到客户端的反馈消息,将重新发送消息. 默认为20秒 2.sy ...

  9. MQTT-C-PUB

    /* ============================================================================ Name        : mqtest ...

  10. iOS应用内支付(IAP)的那些坑

    本文转载至 http://blog.devtang.com/2013/04/07/tricks-in-iap/ 前言 udacity 中的在线课程 <How to build a startup ...