数据结构与算法训练:第二十九弹

讲真要是真的这么考,人真的会无😢
AVL模板!AVL模板!
所谓墨菲定律,越逃避的越可能考到~
话说这次的题目感觉总体难度都不小:指模板属于比较难的,然后灵活的题比较灵活,没找到之前有相似考法的……
得分:70/100 20+19+1+30
耗时:2h30min
知识点:AVL树、CBT树、BST树、大数加法、贪心(emmm)

1. 知识点总结

题目难度知识点
1064 Complete Binary Search Tree🎯🎯层次遍历+中序遍历构建二叉搜索树
1065 A+B and C (64bit)🎯大数加法
1066 Root of AVL Tree😅AVL
1067 Sort with Swap(0, i)🎯✨贪心

2. 分题题解

2.1 1064 Complete Binary Search Tree

首先根据N构建空的满二叉树,然后对输入数组排序后中序遍历将值填入树结构,最后层次遍历输出树节点的值

#include<bits/stdc++.h>
using namespace std;
int N,temp;
struct Node{
	int val;
	Node*left=NULL;
	Node*right=NULL;
};
Node*root;
vector<int>v;
void levelbuild(int n){
	queue<Node*>q;
	int cnt=1;
	root=new Node;
	q.push(root);
	while(!q.empty()){
		Node*top=q.front();
		q.pop();
		if(cnt<n){
			cnt++;
			top->left=new Node;
			q.push(top->left);
		}
		if(cnt<n){
			cnt++;
			top->right=new Node;
			q.push(top->right);
		}
		if(cnt==n){
			break;
		}
	}
}
void levelTravel(Node*root){
	queue<Node*>q;
	q.push(root);
	bool flag=false;
	while(!q.empty()){
		Node*top=q.front();
		q.pop();
		if(!flag){
			flag=true;
		}else{
			printf(" ");
		}
		printf("%d",top->val);
		if(top->left!=NULL){
			q.push(top->left);
		} 
		if(top->right!=NULL){
			q.push(top->right);
		}
	}
} 
int index1=0;
void inTravel(Node*root){
	if(index1>=N){
		return;
	}
	if(root==NULL){
		return;
	}else{
		inTravel(root->left);
		root->val=v[index1];
		index1++;
		inTravel(root->right);
	}
}
int main(){
	scanf("%d",&N);
	v.resize(N);
	//先建立结构 
	levelbuild(N);
	for(int i=0;i<N;i++){
		scanf("%d",&v[i]);
	}
	sort(v.begin(),v.end());
	inTravel(root);
	levelTravel(root);
	return 0;
} 

2.2 1065 A+B and C (64bit)

大数加法和大数比较,因为不会负数运算所以分成了好多特例

#include<bits/stdc++.h>
using namespace std;
string a,b,c,ans;
int N;
bool isUper(string a,string b){
	int lena=a.length();
	int lenb=b.length();
	if(lena==lenb){
		return a>b;
	}else{
		return lena>lenb;
	}
	return false;
}
bool flag;
void Add(string a,string b){
	//正数相加
	ans="";
	int lena=a.length();
	int lenb=b.length();
	int i=0;
	int s=0;
	int c=0;
	for(i=0;i<min(lena,lenb);i++){
		s=c+a[lena-i-1]-'0'+b[lenb-i-1]-'0';
		ans+=s%10+'0';
		c=s/10;
	}
	while(i<lena){
		s=a[lena-i-1]-'0'+c;
		ans+=s%10+'0';
		c=s/10;
		i++;
	}
	while(i<lenb){
		s=b[lenb-i-1]-'0'+c;
		ans+=s%10+'0';
		c=s/10;
		i++;
	}
	while(c){
		ans+=c%10+'0';
		c/=10;
	}
	reverse(ans.begin(),ans.end());
	//cout<<a<<"+"<<b<<"="<<ans<<"\n";
}
int main(){
	cin>>N;
	for(int i=1;i<=N;i++){
		cin>>a>>b>>c;
		if(a[0]=='-'&&b[0]=='-'){
			if(c[0]=='-'){
				Add(a.substr(1),b.substr(1));
				flag=isUper(c.substr(1),ans);
			}else{
				flag=false;
			}
		}else if(a[0]=='-'||b[0]=='-'){
			if(b[0]=='-'){
				swap(a,b);
			}
			if(c[0]=='-'){// -a +b -c
				Add(b,c.substr(1));
				flag=isUper(ans,a.substr(1));
			}else{// -a +b +c
				Add(a.substr(1),c);
				flag=isUper(b,ans);
			}
		}else{//a b 均为 + 
			if(c[0]=='-'){
				flag=true;
			}else{
				Add(a,b);
				flag=isUper(ans,c); 
			}
		}
		printf("Case #%d: ",i);
		if(flag){
			printf("true\n");
		}else{
			printf("false\n");
		}
	}
	return 0;
} 

2.3 1066 Root of AVL Tree

AVL模板题,所以一旦不会就……很凉凉

#include<bits/stdc++.h>
using namespace std;
int N;
vector<int>v;
struct Node{
	int val;
	int height;
	Node*left=NULL;
	Node*right=NULL;
};
Node* newNode(int v){
	Node*node=new Node;
	node->val=v;
	node->height=1;
	return node;
}
int getHeight(Node*root){
	if(root==NULL){
		return 0;
	}else{
		return root->height;
	}
}
void update(Node* &root){
	root->height= max(getHeight(root->left),getHeight(root->right))+1;
}
void L(Node* &root){
	Node* temp=root->right;
	root->right=temp->left;
	temp->left=root;
	update(root);
	update(temp);
	root=temp;
}
void R(Node* &root){
	Node* temp=root->left;
	root->left=temp->right;
	temp->right=root;
	update(root);
	update(temp);
	root=temp;
}
int getD(Node* root){
	return getHeight(root->left)-getHeight(root->right);
}
void insert(Node* &root,int val){
	if(root==NULL){
		root=newNode(val);
		return;
	}
	if(val<root->val){
		insert(root->left,val);
		update(root);
		if(getD(root)==2){
			if(getD(root->left)==1){
			//LL
				R(root); 
			}else if(getD(root->left)==-1){
				L(root->left);
				R(root);
			}
		}
	}else{
		insert(root->right,val);
		update(root);
		if(getD(root)==-2){
			if(getD(root->right)==-1){
			//RR
				L(root); 
			}else if(getD(root->right)==1){
				R(root->right);
				L(root);
			}
		}
	}
}
int main(){
	scanf("%d",&N);
	v.resize(N);
	Node*root=NULL; 
	for(int i=0;i<N;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<N;i++){
		insert(root,v[i]);
	}
	printf("%d",root->val);
	return 0;
}

2.4 1067 Sort with Swap(0, i)

贪心,比较难的题,但是可能想到了v[i]=index这种结构可能会容易解题一点😂(然鹅想到了还是超时了),AC代码参考了柳神的

#include<bits/stdc++.h>
using namespace std;
int N,ans=0;
vector<int>v;
int temp;
int main(){
	scanf("%d",&N);
	v.resize(N);
	for(int i=0;i<N;i++){
		scanf("%d",&temp);
		v[temp]=i;//数字-位置 
	}
	for(int i=1;i<N;i++){
		if(v[i]!=i){
			while(v[0]!=0){
				swap(v[0],v[v[0]]);
				ans++; 
			}
			if(i!=v[i]){
				swap(v[0],v[i]);
				ans++;
			}
		}
	}
	printf("%d",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值