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> ...
随机推荐
- EF Core For Oracle11中Find FirstOrDefault等方法执行失败
问题描述 最近在使用ef core连接oracle的发现Find.FirstOrDefault.Skip Task分页等等方法执行失败.使用的是docker安装的oracle11,错误如下图: 解决办 ...
- Docker实战部署JavaWeb项目-基于SpringBoot
最近在滴滴云上看到服务器很便宜,1核2G,1年只需要68块钱.下面是我基于Docker部署Javaweb服务的过程.目前我见过的最便宜的服务器,阿里云打折的时候都没有这么便宜啊,果断入手.有需要的话可 ...
- Spring boot mvn
https://www.cnblogs.com/xiebq/p/9181517.html https://www.cnblogs.com/sun-yang-/p/7700415.html https: ...
- PHP MySQLi Prepared Statements Tutorial to Prevent SQL Injection
https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection#introduction On ...
- python--终端工具之subprocess
一. subprocess.getstatusoutput import subprocess cmd = 'ifconfig' def cmds(cmd,print_msg=True): statu ...
- QQ全量上云,你想了解的技术细节都在这
腾讯的业务体量非常庞大,在2019年,腾讯已拥有超过了100万台服务器,其中,社交业务包括QQ和空间的体量有近20万台服务器,且分布在全国三地. 把QQ这头大象搬到云上并非易事.作为腾讯最庞大.最悠久 ...
- 请写一个java类,在任何时候都可以向它查询“你已经创建了多少个对象?”
这个问题解决方法很简单,只要设置一个类的静态整型成员(事例中我设置的是n),初始化值为1,然后在其构造函数中添加语句使其+1(n++),这样需要查询创建了多少个对象时直接查询n的值就可以了,如下: p ...
- OpenCV基本绘图函数
线段:line 函数 CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& co ...
- openlayers轨迹匀速播放
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- gulp常用插件之cssnano使用
更多gulp常用插件使用请访问:gulp常用插件汇总 cssnano这是一款将你的 CSS 文件做 多方面的的优化,以确保最终生成的文件 对生产环境来说体积是最小的插件. 更多使用文档请点击访问cha ...