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> ...
随机推荐
- <packaging>pom</packaging>是什么意思
<packaging>pom</packaging>是什么意思? 答: 以下配置<packaging>pom</packaging>的意思是使用mave ...
- 更改 Solution (.Sln) file
Microsoft Visual Studio 2010 的项目为件改为Microsoft Visual Studio 2015默认打开 2010 的Solution (.Sln) file Micr ...
- Windows通过DOS命令行设置IP地址
@rem 设置固定IP地址netsh interface ip set address "本地连接" static 192.168.1.200 255.255.255.0 192. ...
- C#中System.ServiceProgress报错
场景 在C#中检索本地计算机所有服务时,使用 System.ServiceProcess.ServiceController[] services = System.ServiceProcess.Se ...
- srpingboot web - 启动(3) 监听器
接上一篇 一. getRunListeners() 在run() 方法中调用了 getRunListeners(args) 方法, 先看一下这个方法干了什么 private SpringApplica ...
- JS基础-语法+流程控制语句+函数+内置对象【数组】
JS语法: Javascript基本数据类型:undefined/null/Boolean/number/string 复杂数据类型:object typeof用来检测变量的数据类型 typeof的使 ...
- nginx配置location与rewrite规则教程
location 教程 location 教程 示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] }location / { ...
- [SDOI2010]粟粟的书架 [主席树]
[SDOI2010]粟粟的书架 考虑暴力怎么做 显然是提取出来 (x2-x1+1)*(y2-y1+1) 个数字拿出来 然后从大到小排序 然后就可以按次取数了- 然而接下来看数据范围 \(50\%\ r ...
- ElementUI的Table表格添加自定义头CheckBox多选框
在ElmentUI的Table表格组件中,也许你会使用type为selection值的多选框功能,但是此时设置的label属性不生效,不能设置标题名称:有时候我们的需求就是要添加标题名称,那该如何处理 ...
- Java(一)环境的安装与配置
一.JDK的安装 1.JDK与JRE的区别 Java开发环境,简称JDK(Java Development Kit),它是Java的核心,包括了Java编译器.Java运行环境.Java打包工具.Ja ...