Pat 1052 Linked List Sorting (25)
来源:互联网 发布:linux c语言 文件路径 编辑:程序博客网 时间:2024/05/21 10:24
1052. Linked List Sorting (25)
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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 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 100000struct 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)
- PAT-Linked List Sorting (25)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- pat a 1052 Linked List Sorting
- 1052. Linked List Sorting (25)-PAT
- 【PAT】1052. Linked List Sorting (25)
- PAT (Advanced) 1052. Linked List Sorting (25)
- PAT A 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT(A) - 1052. Linked List Sorting (25)
- 【PAT甲级】1052. Linked List Sorting (25)
- PAT A1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25) PAT甲级
- PAT甲级1052. Linked List Sorting (25)
- pat-a1052. Linked List Sorting (25)
- 提交删除操作前confirm
- webpack
- Xcode插件指南
- iOS上传Appstore遇到的问题
- HDU--2034
- Pat 1052 Linked List Sorting (25)
- UNPv2第一章:简介
- 2016.4.27
- C#
- vtk实战(十一)—自定义分隔符读取文本
- java 中文乱码处理
- 前言
- PX4 Offboard Control Using MAVROS on ROS
- Java自动装箱和自动拆箱