PAT (Advanced Level) 1074. Reversing Linked List (25)

简单题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=100000+10;

struct Node
{
    char Address[10];
    char Data[100];
    char Next[10];
} node[maxn],tmp[maxn],pri[maxn];

int n,k;
map<string,int>m;
char StAdd[10];

void Copy(int a,int b)
{
    strcpy(tmp[a].Address,node[b].Address);
    strcpy(tmp[a].Data,node[b].Data);
    strcpy(tmp[a].Next,node[b].Next);
}

int main()
{
    scanf("%s%d%d",StAdd,&n,&k);
    m.clear();
    for(int i=0; i<n; i++)
    {
        scanf("%s%s%s",node[i].Address,node[i].Data,node[i].Next);
        m[node[i].Address]=i;
    }

    int now=m[StAdd];
    int kk=0;
    while(1)
    {
        Copy(kk,now);
        if(strcmp("-1",node[now].Next)==0) break;
        kk++;
        now=m[node[now].Next];
    }

    now=0; int c=0;
    while(now<kk+1)
    {
        if(now+k-1<kk+1)
        {
            for(int i=now+k-1; i>=now; i--)
            {
                strcpy(pri[c].Address,tmp[i].Address);
                strcpy(pri[c].Data,tmp[i].Data);
                c++;
            }
            now=now+k;
        }
        else
        {
            for(int i=now; i<kk+1; i++)
            {
                strcpy(pri[c].Address,tmp[i].Address);
                strcpy(pri[c].Data,tmp[i].Data);
                c++;
            }
            now=kk+1;
        }
    }

    for(int i=0; i<kk+1; i++)
    {
        printf("%s %s ",pri[i].Address,pri[i].Data);
        if(i<kk+1-1) printf("%s\n",pri[i+1].Address);
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-08-28 17:51:33

PAT (Advanced Level) 1074. Reversing Linked List (25)的相关文章

【PAT甲级】1074 Reversing Linked List (25 分)

题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的链表. trick: 测试点6包含一些不在起点这条链表上的结点. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;map<string,pair<int,string> >

PAT 1074. Reversing Linked List (25)

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

1074. Reversing Linked List (25)

reverse 方法很好用 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then yo

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

1074 Reversing Linked List (25分) 链表反转

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

PAT (Advanced Level) 1089. Insert or Merge (25)

简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[maxn],

PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. #include<iostream> #include<cstring> #include<cmath> #include<al

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&