PAT (Advanced Level) Practice 题解代码 - IV (1151-1167)

PAT

PAT (Advanced Level) Practice - IV(1151-1167)

--------------------------------------------------------------------------------
1001-1167(已更新至最新!)

1151-1167

链接:https://pan.baidu.com/s/1JFKRoZO8ZaX_2DE_EOnqVQ
提取码:c5oi
--------------------------------------------------------------------------------
更多详见>>

OJ题解系列 目录导航帖
--------------------------------------------------------------------------------

题目传送门

PAT (Advanced Level) Practice
--------------------------------------------------------------------------------

这里是PAT (Advanced Level) Practice - III(1101-1150)
这是一个全新的起点,我们完成了乙级考试的小目标,向着甲级考试的大目标前进,你将会看到一片新的世界,在数据结构和算法的大海里遨游
首先,我们讲讲两种考试的区别:
1.题面描述不同,乙级考试采用中文描述,甲级考试采用英文描述
2.题目数量不同,乙级考试是5道题,甲级考试是4道题
3.题目的分值不同,乙级考试的分值分布为15/20/20/20/25,甲级考试的分值分布为20/25/25/30
4.难度不同,甲级考试相对乙级考试来说会增加一个难度级别,主要体现在对数据结构的掌握上,如树和图——主要有BST/BBST/CBT/Heap/DAG,算法方面在原有的难度基础之上加深一点(比方说大模拟),引入了一些简单DP
知己知彼方能百战不殆,我们了解基本的区别之后,大家也不用太担心考试的难度问题,毕竟数据规模n不像竞赛要求那么高,很多算法用最普通的枚举方法往往也能取得满分!

接下来就是题解部分了,每道算法题都标注有对应的算法标签,对于那些易错、较难或是测试点比较特殊的题目会着重标注,本章推荐的题目有:

1151 LCA in a Binary Tree (30 分)
1159 Structure of a Binary Tree (30 分)
树,二叉树 + DFS/BFS
1158 Telefraud Detection (25 分) 并查集
1160 Forever (20 分) 数论 + 枚举法

--------------------------------------------------------------------------------

1151 LCA in a Binary Tree (30 分)

算法标签: 树,二叉树 + DFS
注意点: 如下
1.前序+中序遍历建树
2.从根结点出发做DFS遍历,记录路径,求路径的公共最大前缀串
3.判断最大串是否为U、V,输出LCA(U,V)即可。
最后,注意与1143题的异同,由于1151时间复杂度比较宽松(1000ms),方法有很多;1143题加强了时间的限制(200ms),因此DFS遍历需剪枝

#include<bits/stdc++.h>
using namespace std;
int M,N;
int pre[10005];
int in[10005];

struct node{
   
	int data;
	node* lchild;
	node* rchild;
};
node* newnode(int v){
   
	node* Node = new node;
	Node->data = v;
	Node->lchild = NULL;
	Node->rchild = NULL;
	return Node;
}
node* Create(int preL,int preR,int inL,int inR){
   
	if(preL>preR){
   
		return NULL;
	}
	node* root = newnode(pre[preL]);
	int k;
	for(int i=inL;i<=inR;i++){
   
		if(pre[preL] == in[i]){
   
			k = i;
			break;
		}
	}
	int numleft = k-inL;
	root->lchild = Create(preL+1,preL+numleft,inL,inL+numleft-1);
	root->rchild = Create(preL+numleft+1,preR,inL+numleft+1,inR);
	return root;
}

vector<int> ans1;
vector<int> ans2;
vector<int> path1;
vector<int> path2;

int search1(node* root,int v,int depth){
   
	if(root->data == v){
   
		path1.push_back(v);
		ans1 = path1;
		path1.pop_back(); 
		return 1;
	}
	if(root->lchild){
   
		path1.push_back(root->data);
		search1(root->lchild,v,depth+1);
		path1.pop_back();
	}
	if(root->rchild){
   
		path1.push_back(root->data);
		search1(root->rchild,v,depth+1);
		path1.pop_back();	
	}
	return 0;
}
int search2(node* root,int v,int depth){
   
	if(root->data == v){
   
		path2.push_back(v);
		ans2 = path2;
		path2.pop_back(); 
		return 1;
	}
	if(root->lchild){
   
		path2.push_back(root->data);
		search2(root->lchild,v,depth+1);
		path2.pop_back();	
	}
	if(root->rchild){
   
		path2.push_back(root->data);
		search2(root->rchild,v,depth+1);
		path2.pop_back();	
	}
	return 0;
}

int main(){
   
	scanf("%d%d",&M,&N);
	node* root = NULL;
	for(int i=1;i<=N;i++){
   
		scanf("%d",&in[i]);
	}
	for(int i=1;i<=N;i++){
   
		scanf("%d",&pre[i]);
	}
	root = Create(1,N,1,N);
	for(int i=1;i<=M;i++){
   
		int U,V;
		scanf("%d%d",&U,&V);
		path1.clear();
		path2.clear();
		ans1.clear();
		ans2.clear();
		int f1 = search1(root,U,0);
		int f2 = search2(root,V,0);
		if(!ans1.size() && !ans2.size()){
   
			printf("ERROR: %d and %d are not found.\n",U,V);
		}else if(!ans1.size() && ans2.size()){
   
			printf("ERROR: %d is not found.\n",U);
		}else if(ans1.size() && !ans2.size()){
   
			printf("ERROR: %d is not found.\n",V);
		}else{
   
			int k = -1;
			bool f = false;
			for(int i=0;i<ans1.size() && i<ans2.size();i++){
   
				if(ans1[i] == ans2[i]){
   
					f = true;
					continue;
				}else{
   
					k = i-1;
					break;
				}
			}
			if(f && k==-1){
   
				k = min(ans1.size(),ans2.size())-1;
			}
			if(ans1[k] == U){
   
				printf("%d is an ancestor of %d.\n",U,V);
			}else if(ans1[k] == V){
   
				printf("%d is an ancestor of %d.\n",V,U);
			}else{
   
				printf("LCA of %d and %d is %d.\n",U,V,ans1[k]);
			}
		}
	}
	return 0;
} 

1152 Google Recruitment (20 分)

算法标签: 字符串 + 模拟

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int a[maxn];
int L,K;
int isprime(long long a){
   
	for(long long i =2;i<=sqrt(a);i++){
   
		if(a%i==0){
   
			return 0;
		}
	}
	return 1;
}
int main(){
   
	string str;
	cin >> L >> K;
	cin >> str;
	for(int i=0;i<L;i++){
   
		a[i] = str[i] - '0';
	}
	bool flag = false;
	for(int i=0;i<L-K+1;i++){
   
		long long num = 0;
		for(int j=0;j<K;j++){
   
			num = num *10+a[i+j];
		}
		if(isprime(num)){
   
			for(int j=0;j<K;j++){
   
				cout << a[i+j];
			}
			flag = true;
			break;
		}
	}
	if(flag==false){
   
		cout << "404" << endl;
	}
	return 0;
}

1153 Decode Registration Card of PAT (25 分)

算法标签: 字符串 + 排序

#include<bits/stdc++.h>
using namespace std;
struct node {
   
    string t;
    int value;
};
bool cmp(const node &a, const node &b) {
   
    return a.value != b.value ? a.value > b.value : a.t < b.t;
}
int main() {
   
    int n, k, num;
    string s;
    cin >> n >> k;
    vector<node> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i].t >> v[i].value;
    for (int i = 1; i <= k; i++) {
   
        cin >> num >> s;
        printf("Case %d: %d %s\n", i, num, s.c_str());
        vector<node> ans;
        int cnt = 0, sum = 0;
        if (num == 1) {
   
            for (int j = 0; j < n; j++)
                if (v[j].t[0] == s[0]) ans.push_back(v[j]);
        } else if (num == 2) {
   
            for (int j = 0; j < n; j++) {
   
                if (v[j].t.substr(1, 3) == s) {
   
                    cnt++;
                    sum += v[j].value;
                }
            }
            if (cnt != 0) printf("%d %d\n", cnt, sum);
        } else if (num == 3) {
   
            unordered_map<string, int> m;
            for (int j = 0; j < n; j++)
                if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++;
            for (auto it : m) ans.push_back({
   it.first, it.second});
        }
        sort(ans.begin(), ans.end(),cmp);
        for (int j = 0; j < ans.size(); j++) printf("%s %d\n", ans[j].t.c_str(), ans[j].value);
        if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n");
    }
    return 0;
}


1154 Vertex Coloring (25 分)

算法标签: 图论 + 枚举法
注意点: 依次枚举每条边,检查顶点颜色是否相同,即验证是否为合理的染色

#include<bits/stdc++.h>
using namespace std;
int N,M;
int K;
struct node{
   
	int u;
	int v;
};
node edge[10005];
int a[10005];
int main(){
   
	scanf("%d%d",&N,&M);
	for(int i=0;i<M;i++){
   
		scanf("%d%d",&edge[i].u,&edge[i].v);
	}
	scanf("%d",&K);
	for(int i=0;i<K;i++){
   
		set<int> s;
		for(int j=0;j<N;j++){
   
			scanf("%d",&a[j]);
			s.insert(a[j]);
		}
		bool f = true;
		for(int j=0;j<M;j++){
   
			int color1 = a[edge[j].u];
			int color2 = a[edge[j].v];
			if(color1 == color2){
   
				f = false;
				break;
			}
		}
		if(f){
   
			printf("%d-coloring\n",s.size());
		}else{
   
			printf("No\n");
		}
	}
	return 0;
} 

1155 Heap Paths (30 分)

算法标签: 堆 + DFS
注意点:
建堆
DFS遍历至叶结点
打印路径
判断大顶堆/小顶堆/不是堆

#include<bits/stdc++.h>
using namespace std;
int N;
int a[2005];
int path[2005];
void dfs<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值