1133 Splitting A Linked List (25 分)

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

map处理一下都好说。
 1 #include <bits/stdc++.h>
 2 #define N 100005
 3 using namespace std;
 4 string st;
 5 int n,m;
 6 struct Node
 7 {
 8     int val;
 9     string s;
10 }an[N],bn[N],cn[N];
11 map<string,Node> mp;
12 int main(){
13     ios::sync_with_stdio(false);
14     cin.tie(0);
15     cout.tie(0);
16     cin >> st >> n >> m;
17     string x,y;
18     int z;
19     for(int i = 0 ; i < n; i++){
20         cin >> x >> z >> y;
21         mp[x]={z,y};
22     }
23     Node node;
24     int l1 = 0, l2 = 0, l3 = 0;
25     while(st != "-1"){
26         node = mp[st];
27         if(node.val < 0){
28             an[l1++] = {node.val, st};
29         }else if(node.val >= 0 && node.val <= m){
30             bn[l2++] = {node.val, st};
31         }else{
32             cn[l3++] = {node.val, st};
33         }
34         st = node.s;
35     }
36     for(int i = 0; i < l2; i++){
37         an[l1+i] = bn[i];
38     }
39     l1 = l1 + l2;
40     for(int i = 0; i < l3; i++){
41         an[l1+i] = cn[i];
42     }
43     l1 += l3;
44     for(int i = 0; i < l1-1; i++){
45         cout << an[i].s<<" "<<an[i].val<<" "<<an[i+1].s<<endl;
46     }
47     cout << an[l1-1].s<<" "<<an[l1-1].val<<" "<<"-1"<<endl;
48     return 0;
49 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11306470.html

时间: 2024-11-08 12:31:54

1133 Splitting A Linked List (25 分)的相关文章

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

PAT 1133 Splitting A Linked List

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

浙大数据结构课后习题 练习二 7-2 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甲级】1097 Deduplication on a Linked List (25 分)

题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int nex[100007],val[100007]; 5 vector<

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甲级】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> >

A1074 Reversing Linked List (25分)未解决看懂

二.参考代码 #include<cstdio> #include<iostream> using namespace std; int main(){ int first, k, n, sum = 0; cin >> first >> n >> k; int temp, data[100010], next[100010], list[100010], result[100010]; for(int i = 0; i < n; i++){

PAT1133:Splitting A Linked List

1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

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

02-线性结构3 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 ou