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 }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    ddos(分布式拒绝服务)攻击防御措施
    arp_announce和arp_ignore 详细解说
    TCP三次握手和四次挥手
    ARP请求详解
    LVS/DR模式原理剖析(FAQs)
    nfs配置 /etc/exports
    LVS集群之十种调度算法及负载均衡-理论
    SSH 故障排查思路
    shell脚本基础和编写规范
    计算机操作系统概述
  • 原文地址:https://www.cnblogs.com/ldudxy/p/11809806.html
Copyright © 2011-2022 走看看