PAT甲级 1097

  • 题目
  • 解析
  • 代码

题目

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.

解析

输入包含address‘、key、next的数据,将该数据清理干净。key不能重复,也不能和key的绝对值重复。

代码

#include<bits/stdc++.h>#define INF 1<<29using namespace std;struct node {string address;int key;string next;
};
bool flag[10010] = {false};void pat1097() {string begin;int n;map<string, node> data;vector<node> output, remove;cin >> begin >> n;for (int i = 0; i < n; ++i) {string address, next;int key;cin >> address >> key >> next;data[address] = {address, key, next};}while (begin != "-1") {if (!flag[abs(data[begin].key)]) {output.push_back(data[begin]);flag[abs(data[begin].key)] = true;} else {remove.push_back(data[begin]);}begin = data[begin].next;}for (int i = 0; i < output.size(); ++i) {printf("%s %d ", output[i].address.c_str(), output[i].key);if (i != output.size() - 1)printf("%s\n", output[i + 1].address.c_str());elseprintf("-1\n");}for (int i = 0; i < remove.size(); ++i) {printf("%s %d ", remove[i].address.c_str(), remove[i].key);if (i != remove.size() - 1)printf("%s\n", remove[i + 1].address.c_str());elseprintf("-1\n");}
}int main() {pat1097();return 0;
}

PAT甲级 1097相关推荐

  1. PAT甲级1097 Deduplication on a Linked List:[C++题解]遍历链表、两个vector

    文章目录 题目分析 题目链接 题目分析 题意:删掉链表中数值绝对值相同的结点,将其放入另一个链表中.最后输出去重后的链表和删掉元素构成的链表. 分析:采用数组模拟链表,把链表存下来.然后遍历链表,该元 ...

  2. PAT甲级题目对应知识点分类梳理

    PAT甲级的106道题的知识点与对应的题号整理如下,便于做专项练习和巩固! 1.数据结构 可以用STL系列 栈:1051 堆:1098 队列:1014.1056 链表:1032.1052.1074.1 ...

  3. PAT甲级考试题库题目分类

    PAT甲级题目分类 水题 1136.1139.1143.1148 字符串处理 1001.1005.1035.1061.1073.1077.1082. 1108.1140.1152 模拟 1002.10 ...

  4. PAT甲级题目解析和知识点分类整理

    转载请注明出处 个人博客:https://maxusun.github.io/ 今天整理电脑,发现了去年为了考研浙大计算机整理的PAT资料.现在考研已经尘埃落定.想到当时盲目刷题浪费了好多时间,在这里 ...

  5. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

  6. PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy

    PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy 通过:643 提交:1220 通过率:52% Eva would like to make a ...

  7. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  8. Pat甲级 1002 A+B for Polynomials

    Pat甲级 1002 A+B for Polynomials 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems ...

  9. Pat甲级 1001 A+B Format

    Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...

  10. PAT甲级1055 The World‘s Richest:[C++题解]k路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 采用二维数组vector[N]来存每个年龄的人(结构体),然后分别从大到小排序.剩下的任务就是从给定的年龄[a ,b]中,k路归并最 ...

最新文章

  1. python查看微信撤回消息_想查看微信好友撤回的消息?Python帮你搞定
  2. R语言合并两个或多个有序数dataframe实战(dataframe的纵向合并):使用R原生方法、data.table、dplyr等方案
  3. A标签中通过href和onclick传递的this对象实现思路
  4. mysql的一个字段最多能插入多少数据?我们存入text类型的值上限是多大?
  5. python好学吗mooc中文网-2020年大学mooc用Python玩转数据作业答案
  6. 解决MySQL server has gone away
  7. FileLocatorPro正则表达式批量搜索文件中的字符串
  8. ubuntu-10.04硬盘安装挫折略记
  9. java handler null_java – 在调用之前,如何确保另一个Thread的Handler不为null?
  10. Rails IDE 有很多选择,但是具体到ubuntu 64bit 选择的余地就不多了,这里选择Aptana Studio 3 Beta...
  11. 【笔记】Windows Phone 8开发笔记之API
  12. Effective Java~44. 坚持使用标准的函数接口
  13. mysql -- 死锁
  14. android 定制输入法,QQ输入法Android 4.3全新升级 实现私人定制输入
  15. Acey.ExcelX4.2版本发布
  16. 分析reduce()的原理
  17. 基于matlab雷达算法,基于MATLAB的雷达信号处理
  18. 《整理的艺术》读后反思,“强迫症患者”的理论依据
  19. 针对永恒之蓝Wannacry勒索病毒微软Windows操作系统 MS17-010 漏洞补丁资源共享
  20. Python笔记:re模块详解

热门文章

  1. 中国知网论文免费下载的4个方法分享
  2. python tolist()方法
  3. 认识计算机听课记录20篇,【中学信息技术听课记录】 信息技术听课记录15篇及评析_初中信息技术听课记录_高中信息技术听课记录20篇_东城教研...
  4. android小米手机拍照功能,小米手机 调用相机拍照后无法返回问题
  5. (STM32CubeMX)超声波模块测距传感器学习笔记
  6. 机器学习之层次聚类(hierarchical clustering)
  7. c语言读取sgy格式文件,用C语言读写SGY格式地震数据文件.pdf
  8. 获取b站网页html5,教给你4种方法下B站视频,得到真实地址的方法
  9. python_业余学习_分词工具jieba 正则表达式清洗微博文本特殊符号 最大匹配算法
  10. SPSS读取数据出现中文字符显示乱码的解决方案