PTA-链表处理(1074/1032/1052/1097)

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 (≤10 5) 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”要单独输出。

#include<bits/stdc++.h>
using namespace std;
struct node {
	int add,data,next;
	int order;
};
bool cmp(node a,node b) {
	return a.order<b.order;
}
vector<node> vec(100005),ans;
int main() {
	int sta,n,m;
	cin>>sta>>n>>m;
	for(int i=0; i<n; i++) {
		int a,data,b;
		cin>>a>>data>>b;
		vec[a].add=a;
		vec[a].data=data;
		vec[a].next=b;
	}
	int num=0;
	while(sta!=-1) {
		vec[sta].order=++num;
		ans.push_back(vec[sta]);
		sta=vec[sta].next;
	}
	sort(ans.begin(),ans.end(),cmp);
	n=num;
	for(int i=0; i<n/m; i++) {
		for(int j=(i+1)*m-1; j>i*m; j--) {
			printf("%05d %d %05d\n",ans[j].add,ans[j].data,ans[j-1].add);
		}
		printf("%05d %d ",ans[i*m].add,ans[i*m].data);
		if(i<n/m-1) {
			printf("%05d\n",ans[(i+2)*m-1].add);
		} else {
			if(n%m==0) {
				printf("-1");
			} else {
				printf("%05d\n",ans[(i+1)*m].add);
				for(int k=(i+1)*m;k<n;k++){
					printf("%05d %d ",ans[k].add,ans[k].data);
					if(k!=n-1) printf("%05d\n",ans[k+1].add);
					else printf("-1");
				}
			}
		}
	}
	return 0;
}

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.

在这里插入图片描述

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 (≤10 5), 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
whereAddress 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

思路

将第一条头结点的链表中的结点都带上标记,然后从第二个结点开始遍历,遇到的第一个带有标记的结点即是所求,输出地址即可。

#include<bits/stdc++.h>
using namespace std;
struct node{
	char data;
	int next;
	int flag;
};
vector<node> vec(100005);
int main()
{
	int one,two,n;
	cin>>one>>two>>n;
	for(int i=0;i<n;i++){
		int sta,end;
		char data;
		cin>>sta>>data>>end;
		vec[sta].next=end;
		vec[sta].data=data;
		vec[sta].flag=false;
	}
	int num;
	bool pan=false;
	while(one!=-1){
		vec[one].flag=true;
		one=vec[one].next;
	}
	while(two!=-1){
		if(vec[two].flag==true)                      {
			num=two;
			pan=true;
			break;
		}
		two=vec[two].next;
	}
	if(pan) printf("%05d",num);
	else cout<<"-1"<<endl;
	return 0;
}

1052 Linked List Sorting (25 分)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (<10 5) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next
where Address is the address of the node in memory, Key is an integer in [−10 5,10 5], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

思路

这道题的坑点就是输入的所有结点中可能包含不在已经输入结点为头结点的链表上,需要新建一个数组存储去除后这些节点后的完整链表。
之后排序输出地址及数据即可。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int loc;
	int data;
	int next;
};
bool cmp(node a,node b){
	return a.data<b.data;
}
vector<node> vec(100005),ans;
int main()
{
	int n,sta;
	cin>>n>>sta;
	for(int i=0;i<n;i++){
		int a,data,b;
		cin>>a>>data>>b;
		vec[a].data=data;
		vec[a].next=b;
		vec[a].loc=a; 
	}
	while(sta!=-1){
		ans.push_back(vec[sta]);
		sta=vec[sta].next;
	}
	sort(ans.begin(),ans.end(),cmp);
	if(ans.size()==0){
		printf("0 -1");
		return 0;
	}
	printf("%d %05d\n",ans.size(),ans[0].loc);
	for(int i=0;i<ans.size();i++){
		printf("%05d %d ",ans[i].loc,ans[i].data);
		if(i!=ans.size()-1) printf("%05d\n",ans[i+1].loc);
		else printf("-1");	
	}
	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 (≤10 5) 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 10 4, 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

思路

首先是整合,利用头结点遍历输入的所有节点可以排除节点中不在以输入节点为头结点的链表上的结点。
接着用一个哈希表标记链表上的结点,如果没有重复就将它复制到一个空数组yes上,如果重复就按顺序复制到另一个空数组no上。
将yes数组以数据值升序排序,最后按顺序输出yes和no数组即可。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int add,data,next;
};
vector<node> vec(100005),yes,no;
vector<bool> isExist(10001,false);
int main()
{
	int sta,n;
	cin>>sta>>n;
	for(int i=0;i<n;i++){
		int a,data,b;
		cin>>a>>data>>b;
		vec[a].add=a;
		vec[a].data=data;
		vec[a].next=b;
	}
	while(sta!=-1){
		if(isExist[abs(vec[sta].data)]==false){
			yes.push_back(vec[sta]);
			isExist[abs(vec[sta].data)]=true;
		}
		else{
			no.push_back(vec[sta]);
		}
		sta=vec[sta].next;
	}
	for(int i=0;i<yes.size();i++){
		printf("%05d %d ",yes[i].add,yes[i].data);
		if(i!=yes.size()-1) printf("%05d\n",yes[i+1].add);
		else printf("-1\n");
	}
	for(int i=0;i<no.size();i++){
		printf("%05d %d ",no[i].add,no[i].data);
		if(i!=no.size()-1) printf("%05d\n",no[i+1].add);
		else printf("-1\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值