1052 Linked List Sorting (25分)
题目

1. 思路
- 使用map存放所有的地址对
- 使用起始地址遍历map,结果存放在vector中
- 排序vector
- 输出vector
2. 注意点
- 开始的时候起始地址为-1
- 可能有些节点没有用到,注意排除
3. 代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
struct node{
string add;
int score;
node(string _add, int _score){
add = _add;
score = _score;
}
};
bool cmp(node a, node b) {
return a.score < b.score;
}
void print(vector<node> s){
int i=0;
for(i=0;i<s.size()-1;i++){
printf("%s %d %s\n", s[i].add.c_str(), s[i].score, s[i+1].add.c_str());
}
printf("%s %d -1", s[i].add.c_str(), s[i].score);
}
int main() {
vector<node> list;
map<string, pair<string, int> > mp;
int n;
char start[10];
scanf("%d %s", &n, start);
for(int i=0;i<n;i++){
char a[10], b[10];
int num;
scanf("%s %d %s", a, &num, b);
mp[string(a)] = make_pair(string(b), num);
}
string _start = start;
if(_start == "-1"){
printf("0 -1");
return 0;
}
while(_start != "-1"){
pair<string, int> p = mp[_start];
list.push_back(node(_start, p.second));
_start = p.first;
}
sort(list.begin(), list.end(), cmp);
printf("%d %s\n", list.size(), list[0].add.c_str());
print(list);
}
1052 Linked List Sorting (25分)的更多相关文章
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- 面试再问ThreadLocal,别说你不会!
ThreadLocal是什么 以前面试的时候问到ThreadLocal总是一脸懵逼,只知道有这个哥们,不了解他是用来做什么的,更不清楚他的原理了.表面上看他是和多线程,线程同步有关的一个工具类,但其 ...
- C# 如何获取日期时间各种方法
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2019-01-09).时间(16:02:12).日期+时间(2019-01-09 16 ...
- Q&A in 2018 - Q1
Those questions Simply write down questions that ever frustrated me a little: How to convert unix ti ...
- 使用SSM 或者 springboot +mybatis时,对数据库的认证信息(用户名,密码)进行加密。
通常情况下,为了提高安全性,我们需要对数据库的认证信息进行加密操作,然后在启动项目的时候,会自动解密来核对信息是否正确.下面介绍在SSM和springboot项目中分别是怎样实现的. 无论是使用SSM ...
- pyqt5-进度条控制
1.基于自定义类的方式 继承自QProgressBar类,然后重写timerEvent方法,当该组件设置定时器的时候,会自己处理定时的处理方法,完成相应的功能 from PyQt5.Qt import ...
- 【python语法基础-经典练习题】python语法基础练习题01---商场打折
# 1.一家商场在降价促销.如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣(打九折),# 如果购买金额大于100元会给20%折扣.编写一程序,询问购买价格,再显示出折扣(%1 ...
- 0013 基于DRF框架开发(01 基类视图 APIView)
之前学习了模型序列化和普通序列化,我们用最简单的视图和url实现了对序列化的操作. 而实际上,象之前那种由DRF自动生成所有的视图和url的情况,在应用是使用很少.而需要用户根据实际业务需求,自定义视 ...
- pandas 将多个dataframe保存为一个excel文件的多个sheet表中
# 创建文件 def create(): df1 = pd.DataFrame({"a1": [1, 2, 3], "b1": [4, 5, 6]}) df2 ...
- 1级搭建类108-Oracle 11gR2 SI FS(Windows Server 2019)公开
Oracle 11gR2 单实例文件系统在Windows Server 2019上的安装 在线查看
- 路由算法之LS算法和DV算法全面分析
转载文章:https://blog.csdn.net/qq_22238021/article/details/80496138 很透彻!!!