zoukankan      html  css  js  c++  java
  • 【PAT甲级】1074 Reversing Linked List (25 分)

    题意:

    输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址。输出每K个结点局部反转的链表。

    trick:

    测试点6包含一些不在起点这条链表上的结点。

    AAAAAccepted code:

     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 map<string,pair<int,string> >mp;
     5 pair<string,int>ans[100007];
     6 pair<string,int>ans2[100007];
     7 int main(){
     8     ios::sync_with_stdio(false);
     9     cin.tie(NULL);
    10     cout.tie(NULL);
    11     string s;
    12     int n,k;
    13     cin>>s>>n>>k;
    14     for(int i=1;i<=n;++i){
    15         string s1,t1;
    16         int x;
    17         cin>>s1>>x>>t1;
    18         mp[s1]={x,t1};
    19     }
    20     int cnt=0;
    21     while(1){
    22         string now=s;
    23         ans[++cnt]={now,mp[now].first};
    24         s=mp[now].second;
    25         if(s=="-1")
    26             break;
    27     }
    28     int pos=0;
    29     for(int i=1;i*k<=cnt;++i){
    30         pos=i*k;
    31         for(int j=i*k-k+1;j<=i*k;++j)
    32             ans2[j]=ans[(2*i-1)*k+1-j];
    33     }
    34     for(int i=pos+1;i<=cnt;++i)
    35         ans2[i]=ans[i];
    36     for(int i=1;i<=cnt;++i){
    37         cout<<ans2[i].first<<" "<<ans2[i].second<<" ";
    38         if(i<cnt)
    39             cout<<ans2[i+1].first;
    40         else
    41             cout<<-1;
    42         cout<<"
    ";
    43     }
    44     return 0;
    45 }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    在Ubuntu 20.04.2 LTS上,启动samba服务
    怎么将ppt中插入的文件单独保存出来
    两款造包工具,科来和xcap
    intel 网卡 && realtek网卡 抓vlan 设定
    Spring注解和一些类
    ReentrantLock源码阅读
    UG12.0安装
    SQL SERVER 分页代码
    SQL SERVER 处理小数位数函数FU_DecimalDigits
    SQL SERVER 表和列添加备注
  • 原文地址:https://www.cnblogs.com/ldudxy/p/11809806.html
Copyright © 2011-2022 走看看