zoukankan      html  css  js  c++  java
  • 1052 Linked List Sorting (25 分)

    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 (<) 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 −.

    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 [−], 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

    是在下蠢了,写错了一个变量,调了半个小时(哭晕在厕所)。


     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Node
     6 {
     7     string s, ends;
     8     int val;
     9     friend bool operator < (const Node &a, const Node &b){
    10         return a.val < b.val;
    11     }
    12 }node[100005],ans[100005];
    13 map<string, int> mp;
    14 int n;
    15 string ss;
    16 int main(){
    17     cin >> n >> ss;
    18     for(int i = 0; i < n; i++){
    19         cin >> node[i].s >> node[i].val >> node[i].ends;
    20         mp[node[i].s] = i;
    21     }
    22     int p = 0;
    23     while(ss != "-1"){
    24         int a = mp[ss];
    25         ans[p++] = node[a];
    26         ss = node[a].ends;
    27     }
    28     if(p == 0){
    29         cout <<"0 -1"<<endl;
    30         return 0;
    31     }
    32     sort(ans, ans+p);
    33     cout << p << " " << ans[0].s << endl;
    34     for(int i = 0; i < p-1; i++){
    35         cout << ans[i].s <<" "<<ans[i].val << " "<<ans[i+1].s<<endl;
    36     }
    37     cout << ans[p-1].s <<" "<<ans[p-1].val << " "<<"-1"<<endl;
    38     return 0;
    39 }


  • 相关阅读:
    hdu 1588 求f(b) +f(k+b) +f(2k+b) +f((n-1)k +b) 之和 (矩阵快速幂)
    poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵 (矩阵快速幂)
    hdu 1757 和1005差不多 (矩阵快速幂)
    D 矩阵快速幂
    poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)
    hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
    hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
    UVa 1643 Angle and Squares (计算几何)
    UVa 11040 Add bricks in the wall (水题递推)
    UVa 1336 Fixing the Great Wall (区间DP)
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11185665.html
Copyright © 2011-2022 走看看