1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures
according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive
integer. NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no
cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333 33333 100000 -1 巨坑,有链表是空的情况,还有点不再链表中,还有千万别用cin
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <map> using namespace std;
#define MAX 100000
struct Node{
char l[10],r[10];
int key;
}a[MAX+5],e[MAX+5];
int cmp(Node a,Node b){
return a.key<b.key;
}
int n;
char b[10];
map<string,string> m1;
map<string,int> m2;
int main()
{
scanf("%d",&n);scanf("%s",b);
m1.clear();m2.clear();
for(int i=1;i<=n;i++){
scanf("%s%d%s",a[i].l,&a[i].key,a[i].r);
m1[a[i].l]=a[i].r;m2[a[i].l]=i;
}
string c=b;
if(c=="-1"){
cout<<0<<" "<<b<<endl;
return 0;
}
int cnt=0;
while(c!="-1"){
e[++cnt]=a[m2[c]];
c=m1[c];
}
sort(e+1,e+cnt+1,cmp);
cout<<cnt<<" "<<e[1].l<<endl;
for(int i=1;i<=cnt-1;i++)
cout<<e[i].l<<" "<<e[i].key<<" "<<e[i+1].l<<endl;
cout<<e[cnt].l<<" "<<e[cnt].key<<" "<<"-1"<<endl;
return 0;
}

Pat 1052 Linked List Sorting (25)的更多相关文章

  1. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  2. 【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 ...

  3. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  4. PAT 1052 Linked List Sorting [一般]

    1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...

  5. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  6. PAT Advanced 1052 Linked List Sorting (25) [链表]

    题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...

  7. PAT (Advanced Level) 1052. Linked List Sorting (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  9. 1052. Linked List Sorting (25)

    题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...

随机推荐

  1. SQL语句创建相同结构的表

    --Oracle的语句create table sa_salaryRecord as select * from sa_salary where 1=2; --MSSQL的语句select * int ...

  2. Linux系统编程之----》信号

    "===信号========================================================================================= ...

  3. mysqli常用方法

    //返回前一次 MySQL 操作(SELECT.INSERT.UPDATE.REPLACE.DELETE)所影响的记录行数. //一个 > 0 的整数表示所影响的记录行数.0 表示没有受影响的记 ...

  4. c#.net调用pdf2swf.exe将pdf文件转换为swf,vs中运行正常,布署IIS服务器部署转换后文字部分为空白

    这个是权限问题, 需要在应用程序池中高级设置,将标识改为LocalSystem

  5. 查看selinux的状态

    sh-4.1# getenforce Disabled sh-4.1# getenforce Disabled 永久方法 – 需要重启服务器 修改/etc/selinux/config文件中设置SEL ...

  6. Fatal error compiling: 无效的标记: -parameters

    [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ ...

  7. 把一个project相关的jar放到project的lib文件夹中

    例如你有一个project名为xxx,/xxx 文件夹中有src.bin.等等,然后你手工创建一个lib文件夹,这样你就把解压出来的第三方lib(jar包,含javadoc)都放到lib文件夹中,这样 ...

  8. PL/SQL Developer导入、导出表结构和表数据

    在表的所有者不能改变的情况下,可以使用导入导出表结构和表数据的方法,将表移动到你想要的所有者下(注:特别是建立表的时候如果以sysdba的身份登录的话,所有表的所有者都为sys,此时会对你用c#访问数 ...

  9. Unix系统编程()main函数的命令行参数

    命令行参数输入双引号是什么效果? 好像可以去空格化.

  10. dubbo配置约束

    此处主要记录dubbo配置的一些约束规则. 采用官网提供的原文,描述如下: 一.XML配置(官网原文) 以 timeout 为例: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费方优先 ...