zoukankan      html  css  js  c++  java
  • pat 1133

    1133 Splitting A Linked List (25分)
     

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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

    Address Data Next
    
     

    where Address is the position of the node, Data is an integer in [, and Next is the position of the next node. It is guaranteed that the list is not empty.

    Output Specification:

    For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input:

    00100 9 10
    23333 10 27777
    00000 0 99999
    00100 18 12309
    68237 -6 23333
    33218 -4 00000
    48652 -2 -1
    99999 5 68237
    27777 11 48652
    12309 7 33218
    
     

    Sample Output:

    33218 -4 68237
    68237 -6 48652
    48652 -2 12309
    12309 7 00000
    00000 0 99999
    99999 5 23333
    23333 10 00100
    00100 18 27777
    27777 11 -1

    题意:给定n个结点,对其按照关键字排序,要求关键字为负数排在前面,其次是[0,k]范围内的数,最后是大于k的数,要求排序不改变相同类型的结点的相对位置。
    思路:首先用hash的方法读入关键字,开一个大小为100005的数组,数组下标表示地址,读入的时候往里面存入关键字。再开一个nextNode[100005]的数组存放地址为addr的结点的下一个结点的地址。从根节点开始遍历,遍历三遍,第一次把所有负的结点加入vector数组,第二次[0,k]范围的结点,最后一次大于k的结点。
    注意:有些结点可能不在链表中,因此要算一下链表中结点的个数,最后输出的时候按照链表中结点的个数进行输出。

    代码如下
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    using namespace std;
    vector<int> head,tail,dat;
    int node[100005];
    int nextNode[100005];
    int main(){
        fill(nextNode,nextNode+100005,-1);
        int root,n,k;
        scanf("%d%d%d",&root,&n,&k);
        int addr,data,nex;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&addr,&data,&nex);
            node[addr]=data;
            nextNode[addr]=nex;
        }
        int i=root;
        int cnt=0;
        while(i!=-1) {
            i=nextNode[i];
            cnt++;
        }
        i=root;
        while(i!=-1){
            if(node[i]<0){
                head.push_back(i);
                dat.push_back(node[i]);
            }
            i=nextNode[i];
        }
        i=root;
        while(i!=-1){
            if(node[i]>=0&&node[i]<=k){
                head.push_back(i);
                dat.push_back(node[i]);
            }
            i=nextNode[i];
        }
        i=root;
        while(i!=-1){
            if(node[i]>k){
                head.push_back(i);
                dat.push_back(node[i]);
            }
            i=nextNode[i];
        }
        for(int i=0;i<cnt;i++){
            if(i!=cnt-1){
                printf("%05d %d %05d
    ",head[i],dat[i],head[i+1]);
            }
            else
                printf("%05d %d -1
    ",head[i],dat[i]);
            
        }
        return 0;
    }
     
  • 相关阅读:
    rest framework 认证 权限 频率
    rest framework 视图,路由
    rest framework 序列化
    10.3 Vue 路由系统
    10.4 Vue 父子传值
    10.2 Vue 环境安装
    10.1 ES6 的新增特性以及简单语法
    Django 跨域请求处理
    20190827 On Java8 第十四章 流式编程
    20190825 On Java8 第十三章 函数式编程
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13269000.html
Copyright © 2011-2022 走看看