PAT甲级_2022夏(AK)

总结

第四次模考,重刷了一遍PAT甲级题库,终于第一次AK。
在这里插入图片描述

第一题

题意

给定两个孩子对昨天、今天、明天的猜测,他们各有且仅有一个是对的,分析今天是周几并输出两个孩子猜对了那一天。

思路

  • 记录两个孩子对3天的猜测放到数组a、b。
  • 对所有排列组合[i, j]进行分析,若(i - j + 7) % 7 == (a[i] - b[j] + 7) % 7则该组合可能是目标组合。
  • 再判断这个组合对应结果满不满足其他四个猜测为错,若是则输出。

小结

  • debug很长时间,最后发现数组名写重复了。
  • 取模方法:(a - b + m) % m

题解

#include<bits/stdc++.h>
using namespace std;
int a[3], b[3], ans;
string tmp[3] = {"yesterday", "today", "tomorrow"};
string week[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int main(){
    for(int i = 0; i < 3; i ++) cin>>a[i];
    for(int j = 0; j < 3; j ++) cin>>b[j];
    bool flag = false;
    for(int i = 0; i < 3; i ++){
    	for(int j = 0; j < 3; j ++){
    		if((i - j + 7) % 7 == (a[i] - b[j] + 7) % 7){
    			flag = false;
    			ans = (1 - i + a[i] + 7) % 7;
    			for(int k = 0; k < 3; k ++)
    				if(k != i && ((a[k] - a[i] + 7)%7 == (k - i + 7) % 7)) flag = true;
				for(int k = 0; k < 3; k ++)
					if(k != j && ((b[k] - b[j] + 7)%7 == (k - j + 7) % 7)) flag = true;
				if(flag == true) continue;
				else{
					cout<<week[ans]<<endl<<tmp[i]<<endl<<tmp[j];
					return 0;
				}
			}
		}
	}
    return 0;
}

第二题

40min

题意

给定cache大小n,序列长度m,然后依次给出m个数字代表页号,模拟LRU替换策略。顺序输出替换出来的页号。

思路

  • set<pair<int, int>> cache记录cache中[页号进入时间, 页号]deque<int> ans记录替换出来的页号。
  • 用数组place记录页号进入的时间。初始时间从1开始算起,最大时间为m。
  • 页号page依次进入cache:
    • 如果place[page] != 0说明page在cache中,这时候删除原数据,修改place再插入新数据到cache。
    • 如果cache.size() < n 直接修改place再插入新数据到cache。
    • 否则,弹出cache中第一个元素到ans数组。修改place。然后插入新数据,修改place。
  • 顺序输出ans数组。

小结

  • set、map作为红黑树,插入删除时间复杂度为O(log_2n)
  • 虽然map存的是pair数组,但是查找输入key就可以了比如rec.erase(key),而不是{key, value}一起输入。
  • 对于堆(priority_queue)也可以,但是这题有删除堆顶元素以外元素的操作,所以堆得自己手写,不能stl。第一次这块儿用的优先队列,写到一半才发现,只得推倒重来。

题解(AC)

#include<bits/stdc++.h>
using namespace std;
int n, m, tmp;
priority_queue<pair<int, int>> que;
deque<int> ans;
set<pair<int, int>> cache;
int place[20010], len;
int main(){
	cin>>n>>m;
	for(int i = 1; i <= m; i ++){
		cin>>tmp;
		if(place[tmp] != 0){
			cache.erase(cache.find({place[tmp], tmp}));
			place[tmp] = i;
			cache.insert({i, tmp});
		}else if(cache.size() < n){
			cache.insert({i, tmp});
			place[tmp] = i;	
		}
		else{
			auto t = cache.begin();
			place[t->second] = 0;
			ans.push_back(t->second);
			cache.erase(t);
			cache.insert({i, tmp});
			place[tmp] = i;
		}
	}
	for(int i = 0; i < ans.size(); i ++){
		if(i) printf(" ");
		printf("%d", ans[i]);
	}
}

题解 - 待测试

#include<bits/stdc++.h>
using namespace std;
int n, m, tmp;
deque<int> ans;
map<int, int> cache;
int place[20010], len;
int main(){
	cin>>n>>m;
	for(int i = 1; i <= m; i ++){
		cin>>tmp;
		if(place[tmp] != 0) cache.erase(place[tmp]);
		else if(cache.size() >= n){
			auto t = cache.begin();
			place[t->second] = 0;
			ans.push_back(t->second);
			cache.erase(t);
		}
		place[tmp] = i;
		cache[i] = tmp;
	}
	for(int i = 0; i < ans.size(); i ++){
		if(i) printf(" ");
		printf("%d", ans[i]);
	}
}

第三题

30min

题意

给出结点数n、边数m、询问次数k。依次给出m条边,再给出k条路径。问这k条路径分别是否是DFS可以得到的遍历序列。

思路

  • 计算内存上限,确定可以使用邻接矩阵,构造bool g[1001][1001]记录图。
  • 循环k次,每次根据test函数判断该次给出序列是否正确。
  • 构造test函数:用deque<int> rec记录给出序列,初始化st数组用于记录是否访问过目标节点。对rec数组进行dfsTraverse检测。
  • dfs(int s, bool &f)方法:
    • 每次进来就rec.pop()弹出头部元素s,记录访问过。
    • 然后判断如果有一条s到rec.front()的路径,则dfs进入。
    • 退出时(没有s到rec.front()的路径),如果发现下一个元素是被访问过的(rec中元素重复)则test返回false。同时如果s有到其他节点的路径,则test返回false。

小结

  • 耗时较长,其实写出上述逻辑步骤也挺快的,逻辑清晰。以后可以尝试先写逻辑步骤,然后再实现,也许会快一点。

题解

#include<bits/stdc++.h>
using namespace std;
int n, m, k, a, b;
bool flag, st[1001], g[1001][1001];
deque<int> rec;
void dfs(int s, bool& f){
	if(!f)return;
	rec.pop_front();
	st[s] = true;
	while(f && rec.size() && !st[rec.front()] && g[s][rec.front()]) dfs(rec.front(), f);
	if(!f ||(rec.size() && st[rec.front()])){
		f = false;
		return;
	}
	for(int i = 1; i <= n; i ++){
		if(!st[i] && g[s][i]){
			f = false;
			return;
		}
	}
}
bool test(){
	rec.resize(n);
	for(int i = 0; i < n; i ++) scanf("%d", &rec[i]);
	memset(st, false, sizeof st);
	flag = true;
	while(rec.size()){
		dfs(rec[0], flag);
		if(!flag)return false;
	}
	return true;
}
int main(){
	cin>>n>>m>>k;
	rec.resize(n);
	while(m--){
		cin>>a>>b;
		g[a][b] = true;
	}
	while(k--) puts(test() ? "yes" : "no");
	return 0;
}

第四题

80min

题意

给出结点数n,度数d。以此构造度为d的完全树。输出层序遍历。然后给出询问次数k,每次询问层序遍历中某位置开始到根的路径。

思路

  • 首先接收节点数n,度数d,将先序遍历序列记录到数组tree中。
  • 然后计算最后一层数量remain,和总高度h
  • 根据以上数据和完全树的特性,先序遍历构造树,同时用哈希表关联key和node,用传参关联node和其父节点。
  • 层序遍历构造好的树,将输出放到out数组,最后一并输出。
  • 然后接收询问次数k,每次通过起点向根寻找,输出路径。

小结

  • 耗时长,对先序遍历不熟悉。同时要注意熟练使用哈希表简化逻辑。

题解

#include<bits/stdc++.h>
using namespace std;
int n, d, k, a[52], h = 1, ceiling = 1, remain = 0, ptr, cnt;
struct node{
	int k;
	struct node* p;
	struct node* child[5];
};
vector<deque<int>> rec;
unordered_map<int, node*> par;
deque<int> tree;
node* mt(int hi, node* p){
	if(hi > h || (hi == h && cnt == remain)) return NULL;
	node * t = (node*)malloc(sizeof(node));
	t->k = tree.front();
	t->p = p;
	par[t->k] = t;
	tree.pop_front();
	if(hi == h){
		cnt++;
		for(auto & i : t->child) i = NULL;
		return t;
	}
	for(int i = 0; i < d; i ++) t->child[i] = mt(hi + 1, t);
	for(int i = d; i < 5; i ++) t->child[i] = NULL;
	return t;
}
int main(){
	cin>>n>>d;
	for(int i = 1; i <= n; i ++) cin>>a[i];
	for(int i = 1; i <= n; i ++) tree.push_back(a[i]);
	while(ceiling < n){
		h++;
		ceiling += pow(d, h - 1);
	}
	remain = n - (ceiling - pow(d, h - 1));
	node *t = mt(1, NULL);
	queue<node*> ans;
	ans.push(t);
	vector<int> out , tmp;
	while(ans.size()){
		auto tar = ans.front();
		ans.pop();
		out.push_back(tar->k);
		for(int i = 0; i < d; i++)
			if(tar->child[i] != NULL) ans.push(tar->child[i]);
	}
	for(int i = 0; i < out.size(); i ++) printf("%d%c", out[i], i == out.size() - 1 ? '\n' : ' ');
	cin>>k;
	while(k--){
		int pt; cin>>pt;
		tmp.clear();
		node * s = par[out[pt]];
		while(s != NULL){
			tmp.push_back(s->k);
			s = s->p;
		}
		for(int j = 0; j < tmp.size(); j ++) printf("%d%c", tmp[j], j == tmp.size() - 1 ? '\n' : ' ');
	}
	return 0;
}

7-1 What Day is Today

Amy and Tom are arguing about what day is today. Their conversations are as the following:

Amy: Today is Friday.
Tom: No! Today is Saturday.
Amy: But Wednesday was yesterday.
Tom: No way! Yesterday was Thursday.
Amy: Then tomorrow must be Tuesday.
Tom: Are you kidding? It's Monday tomorrow.

Now their mother tells you that each of them has said only one thing correct. It’s your job to tell exactly what day is today.

Input Specification:

Each input file contains one test case. Each case consists of 2 lines, each gives the claims of a kid, in the following format:

yesterday today tomorrow

where the days are represented by numbers from 0 to 6, corresponding to Sunday through Saturday.

Output Specification:

First output in a line the English name for “today”. It is guaranteed that each kid has said only one thing correct, and there is a unique answer for “today”.

Then in the next two lines, print in order the correct days (either yesterday or today, or tomorrow) obtained from the kids.

Sample Input:

3 5 2
4 6 1

Sample Output:

Friday
today
yesterday

Note: The English names for the days are

0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday

7-2 Least Recently Used Cache

Least Recently Used (LRU) cache scheme is to remove the least recently used frame (the one hasn’t been used for the longest amount of time) when the cache is full and a new page is referenced which is not there in cache.

Your job is to implement this LRU cache scheme.

Input Specification:

Each input file contains one test case. For each case, the first line gives 2 positive integers N (≤104) and M (≤105) which are the size of the cache and the number of referenced page ID’s. Then M referenced page ID’s are given in the next line. A page ID is a number in the range [1,2×104]. All the numbers in a line are separated by a space.

Output Specification:

For each test case, output in a line the page ID’s in the order of their being removed from the cache. All the numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

It is guaranteed that at least one page will be removed.

Sample Input:

4 11
1 2 3 1 4 5 2 1 5 6 3

Sample Output:

2 3 4 2

Hint:

When pages with ID’s 1, 2, and 3 comes in order, they are all stored in the cache and page 1 is now the LRU frame.

When page 1 is accessed, page 2 then becomes the LRU frame.

Page 4 can still be stored in the cache, and now the cache is full. When page 5 is to be cached, page 2 will be removed and page 3 becomes the LRU frame.

When the next page 2 comes in, page 3 is removed and page 1 becomes the LRU frame.

Then page 1 is accessed, so page 4 becomes the LRU frame.

When page 5 is accessed, the LRU frame is not changed. Then page 6 comes in and page 4 is removed. The LRU frame is now page 2.

Finally page 2 is removed when page 3 comes in.

7-3 DFS Sequence

dfs.jpg

The above question is commonly seen in a final exam of the course Data Structures. That is, given a graph G, you are supposed to tell if a sequence of vertices can be possibly obtained from any depth first search (DFS) on G.

Now it is your job to write a program which can give the answers automatically.

Note: it is required that each vertex must be visited once even if the graph is not connected. In the graph given by the sample input, if we start from vertex 4, then vertex 1 cannot be reached during this DFS, yet it can be the first vertex visited in the next DFS. Hence the 5th query 4 3 2 5 1 is possible.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive integers: N (≤103), M (≤104), and K (≤100), which are the number of vertices, the number of edges, and the number of queries, respectively. Then M lines follow, each gives an edge in the format:

source destination

which means that there is an edge from source vertex to destination vertex. Here we assume that all the vertices are numbered from 1 to N. It is guaranteed that source is never the same as destination.

Finally K lines of queries are given, each contains N vertices. The numbers in a line are separated by spaces.

Output Specification:

For each query, print in a line yes if the given sequence does correspond to a DFS sequence, or no if not.

Sample Input:

5 7 6
1 2
1 3
1 5
2 5
3 2
4 3
5 4
1 5 4 3 2
1 3 2 5 4
1 2 5 4 3
1 2 3 4 5
4 3 2 5 1
5 4 3 2 5

Sample Output:

yes
yes
yes
no
yes
no

7-4 Complete D-Tree

A d**-tree** is a tree of degree d. A complete tree is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Given the pre-order traversal sequence of a complete d-tree, you are supposed to obtain its level-order traversal sequence. And more, for any given node, you must be able to output the bottom-up path from this node to the root.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: N (≤50) and d (2≤d≤5), which are the total number of nodes in the tree, and the tree’s degree, respectively. Then in the next line, N integer keys (≤100) are given as the pre-order traversal sequence of the tree. Finally K (≤N) and K positions are given, where each position is the index of a key in the level-order traversal sequence, starting from 0.

All the numbers in a line are separated by a space.

Output Specification:

For each case, first print in one line the level-order traversal sequence of the complete d-tree. Then for each given position, print in a line the bottom-up path from this node to the root.

All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

9 3
91 71 2 34 10 15 55 18 7
3
5 7 3

Sample Output:

91 71 15 7 2 34 10 55 18
34 71 91
55 15 91
7 91
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值