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 }


  • 相关阅读:
    git log
    [转]深入详解javascript之delete操作符
    js性能优化文章集锦
    [转]A记录和CNAME记录的区别
    [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!
    nodejs渲染到页面的理解
    git之reset
    Mayavi入门
    OpenAcc笔记——update
    Qt笔记——数据库的图形界面
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11185665.html
Copyright © 2011-2022 走看看