• 02-线性结构3 Reversing Linked List


    02-线性结构3 Reversing Linked List   (25分)

    • 时间限制:400ms
    • 内存限制:64MB
    • 代码长度限制:16kB
    • 判题程序:系统默认
    • 作者:陈越
    • 单位:浙江大学

    https://pta.patest.cn/pta/test/3512/exam/4/question/62614

    Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=4, you must output 4→3→2→1→5→6.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NNN (≤105le 10^5105​​) which is the total number of nodes, and a positive KKK (≤Nle NN) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

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

    Address Data Next
    

    where Address is the position of the node, Data is an integer, and Next is the position of the next node.

    Output Specification:

    For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input:

    00100 6 4
    00000 4 99999
    00100 1 12309
    68237 6 -1
    33218 3 00000
    99999 5 68237
    12309 2 33218
    

    Sample Output:

    00000 4 33218
    33218 3 12309
    12309 2 00100
    00100 1 99999
    99999 5 68237
    68237 6 -1

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct Node
     5 {
     6     int data;
     7     int adr,next;
     8 }t;
     9 int N,head,K;
    10 map<int,Node> Input;//用map储存输入序列,地址作为键值,方便通过地址(head,next)找到相关点
    11 stack<Node> Sta,T;
    12 vector<Node> Result(N);//储存结果链表
    13 vector<Node>::iterator it;
    14 int main()
    15 {
    16     /*输入*/
    17     scanf("%d %d %d",&head,&N,&K);
    18     for(int i=0;i<N;i++)
    19     {
    20         scanf("%d %d %d",&t.adr,&t.data,&t.next);
    21         Input[t.adr]=t;
    22     }
    23     /*处理*/
    24     int num=0;
    25     t=Input.find(head)->second;//找到第一个点
    26     while(num++<N)//对n个元素入栈处理
    27     {
    28         int flag=t.next;
    29         Sta.push(t);
    30         if(t.next!=-1)
    31             t=Input.find(t.next)->second;
    32         if(num%K==0)//满K个元素,逆序出栈放入Result
    33         {
    34             while(!Sta.empty())
    35             {
    36                 Result.push_back(Sta.top());
    37                 Sta.pop();
    38             }
    39             if(flag==-1)//最后一个结点刚好组成最后K个,结束
    40                 break;
    41             continue;
    42         }
    43         else if(flag==-1)//最后一个结点多余,将栈中的点出,入,出栈后恢复正序放入Result,结束
    44         {
    45             while(!Sta.empty())
    46             {
    47                 T.push(Sta.top());
    48                 Sta.pop();
    49             }
    50             while(!T.empty())
    51             {
    52                 Result.push_back(T.top());
    53                 T.pop();
    54             }
    55             break;
    56         }
    57     }
    58     /*输出*/
    59     it=Result.begin();
    60     printf("%05d %d ",it->adr,it->data);
    61     it++;
    62     for(;it!=Result.end();it++)
    63     {
    64         printf("%05d
    %05d %d ",it->adr,it->adr,it->data);
    65     }
    66     printf("-1
    ");
    67     Input.clear();
    68     Result.clear();
    69     return 0;
    70 }
  • 相关阅读:
    Linux系统中如何查找大文件或目录文件夹的方法
    SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled) 几种模式之间的转换
    使用纯css代码实现div的“回”字型“叠放”效果
    中国剩余定理求解“六位教授必须首次都停止上课”问题
    Java基础篇Socket网络编程中的应用实例
    计算正互反矩阵的特征值及特征向量
    维吉尼亚密码java代码实现根据密钥长度计算IC值过程
    维吉尼亚密码java完整版
    容器常用操作注意事项
    HashMap源码分析
  • 原文地址:https://www.cnblogs.com/Fresh--air/p/6723511.html
Copyright © 2020-2023  润新知