PAT甲级中的链表题:
CSDN
相关题目
PAT甲级中的链表题
1032 Sharing
分数 25
To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading
and being
are stored as showed in Figure 1.
You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1).
Input Specification:
Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is the letter contained by this node which is an English letter chosen from { a-z, A-Z }, and Next
is the position of the next node.
Output Specification:
For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output -1
instead.
Sample Input 1:
11111 22222 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010
Sample Output 1:
67890
Sample Input 2:
00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1
Sample Output 2:
-1
题解
1.用一个ne数组来存储每个节点的下一个节点下标
2.先遍历第一个单词,然后遍历第二个单词,如果出现过这个字母则直接返回改节点编号,否则为-1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5 + 5;
int start1, start2, n;
bool st[N];
int ne[N];
int run(){
for(int i = start2; i != -1; i = ne[i]){
if(st[i] == true) return i;
}
return -1;
}
int main(){
cin >> start1 >> start2 >> n;
for(int i = 0, l, r; i < n; i ++ ){
char c;
cin >> l >> c >> r;
ne[l] = r;
}
for(int i = start1; i != -1; i = ne[i]) st[i] = true;
int res = run();
if(res != -1) printf("%05d", res);
else printf("-1");
return 0;
}
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:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) 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 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, 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.对链表遍历一遍存入vector中
2.每k个进行翻转,最后如果不足k个就保留
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 1E5 + 5;
int init, n, k;
int e[N], ne[N];
vector<int>vec;
int main(){
cin >> init >> n >> k;
for(int i = 1; i <= n; i ++ ){
int a, b, c;
cin >> a >> b >> c;
e[a] = b, ne[a] = c;
}
for(int i = init; i != -1; i = ne[i]) vec.push_back(i);
for(int i = 0, len = vec.size(); i + k <= len; i += k){
reverse(vec.begin() + i, vec.begin() + i + k);
}
for(int i = 0, len = vec.size(); i < len; i ++ ){
printf("%05d %d ", vec[i], e[vec[i]]);
if(i != len - 1) printf("%05d\n", vec[i + 1]);
else printf("-1\n");
}
return 0;
}
1097 Deduplication on a Linked List
分数 25
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤105) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the position of the node, Key is an integer of which absolute value is no more than 104, and Next is the position of the next node.
Output Specification:
For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
Sample Output:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
题解
1.定义2个vector容器res,del;分别存储删除后的链表和需要删除的链表
2.然后按照格式输出
#include<iostream>
#include<vector>
using namespace std;
const int N = 1E5 + 5;
int init, n;
int e[N], ne[N];
bool st[N];
vector<int>res, del;
int main(){
cin >> init >> n;
for(int i = 1; i <= n; i ++ ){
int a, b, c;
cin >> a >> b >> c;
e[a] = b, ne[a] = c;
}
for(int i = init; i != -1; i = ne[i]){
int val = abs(e[i]);
if(st[val]) del.push_back(i);
else res.push_back(i);
st[val] = true;
}
for(int i = 0, len = res.size(); i < len; i ++ ){
printf("%05d %d ", res[i], e[res[i]]);
if(i != len - 1) printf("%05d\n", res[i + 1]);
else printf("-1\n");
}
for(int i = 0, len = del.size(); i < len; i ++ ){
printf("%05d %d ", del[i], e[del[i]]);
if(i != len - 1) printf("%05d\n", del[i + 1]);
else printf("-1\n");
}
return 0;
}
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 (≤105) which is the total number of nodes, and a positive K (≤103). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
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 [−105,105], 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
题解
1.分别判断链表中的数是否小于0,位于[0,k]之间,大于k;然后依次存入vector中
2.最后依次输出即可
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
const int N = 1E5 + 5;
int init, n, k;
int e[N], ne[N];
vector<int>vec;
unordered_map<int,bool>st;
int main(){
cin >> init >> n >> k;
for(int i = 1; i <= n; i ++ ){
int a, b, c;
cin >> a >> b >> c;
e[a] = b, ne[a] = c;
}
for(int i = init; i != -1; i = ne[i]){
int val = e[i];
if(val < 0){
vec.push_back(i);
st[val] = true;
}
}
for(int i = init; i != -1; i = ne[i]){
int val = e[i];
if(val >= 0 && val <= k){
vec.push_back(i);
st[val] = true;
}
}
for(int i = init; i != -1; i = ne[i]){
int val = e[i];
if(!st[val]) vec.push_back(i);
}
for(int i = 0, len = vec.size(); i < len; i ++ ){
printf("%05d %d ", vec[i], e[vec[i]]);
if(i != len - 1) printf("%05d\n", vec[i + 1]);
else printf("-1\n");
}
return 0;
}