UVA - 712 S-Trees

本文解析了一个UVA在线评测平台上的题目,该题涉及完全二叉树的遍历及节点路径编码问题。通过输入节点的访问顺序和特定的01字符串来确定每个访问序列对应的二进制值,最终输出每个序列对应的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=653

Sample Input

3

x1 x2 x3

00000111

4

000

010

111

110

3

x3 x1 x2

00010011

4

000

010

111

110

0

Sample Output

S-Tree #1: 0011

S-Tree #2: 0011

题解:完全二叉树,数据不大,用编号判断即可。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N = 5050;
int x[N];
char s[N];
char dep[N];
int ans[N];
int main(){
    int time=0;
    int n;
    while(~scanf("%d",&n) && n){
    	getchar();
    	for(int i=0;i<n;i++){
    		getchar();
    		scanf("%d",&x[i]);
    		getchar();
		}
		scanf("%s",s);
		long long bian=pow(2,n);
//		printf("%d\n",bian);
        int count;
    	scanf("%d",&count);
		for(int i=0;i<count;i++){
			scanf("%s",dep);
			long long ans1=1;
			for(int j=0;j<n;j++){
				if(dep[x[j]-1]=='0')
				    ans1=2*ans1; 
				if(dep[x[j]-1]=='1')
				    ans1=ans1*2+1;
			}
			ans[i]=s[ans1-bian]-'0';
		} 
    	printf("S-Tree #%d:\n",++time);
    	for(int i=0;i<count;i++)
    	    printf("%d",ans[i]);
    	printf("\n");
    	printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值