UVa712 - S-Trees

题意

给一棵满二叉树,每一层有一个变量,每个变量都有一个值:0或1。0向左走,1向右走。求到达叶子的值

思路

不用建树,只要模拟左走右走就可以了

总结

建树应该也能做,但是目前对建树有点生疏,不是很会。

在推左走右走的公式时能快一点最好,明明很容易,却找共性找了很久,而且DEBUG了很久,就是因为在存叶子那行数值的时候存的字符型,而不是整型,忘记 - ‘0’,时间都浪费在这里了。

这次一次就过了哦,继续努力,加油。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <stack>
 6 using namespace std;
 7 const int maxn = 10;
 8 int n, m, kase = 0;
 9
10 int main()
11 {
12    // freopen("in.txt","r",stdin);
13     while(cin >> n && n){
14         cout << "S-Tree #" << ++kase << ":" << endl;
15         int node[maxn];
16         getchar();
17         string s, ter, val;
18         getline(cin,s);
19         int len = s.size();
20         int num = 0;
21         for(int i = 0; i < len; i++)
22             if(isdigit(s[i])) node[num++] = s[i]-‘0‘;
23         cin >> ter >> m;
24         getchar();
25         for(int i = 0; i < m; i++){
26             cin >> val;
27             int cnt = 1;
28             for(int j = 0; j < n; j++){ //模拟
29                 if(val[node[j]-1]-‘0‘ == 1) cnt *= 2;
30                 else cnt = cnt*2-1;
31             }
32             cout << ter[cnt-1];
33         }
34         cout << endl;
35         cout << endl;
36     }
37     return 0;
38 }
时间: 2024-11-04 04:02:35

UVa712 - S-Trees的相关文章

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2

HDOJ 4010 Query on The Trees LCT

LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2582    Accepted Submission(s): 1208 Problem Description We have met so many problems

96. Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 对1-n组成的BST,如果选定i为根,那1-(i-1)都在根的左子树里,(i+1)-n都在根的右子树里

[leetcode-95-Unique Binary Search Trees II]

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: This probl

Unique Binary Search Trees I &amp; II

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 分析: 当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其

leetcode95 Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 思路: 本题采取递归的思路. 传递的参数是开始数值(begin)和结束数值(end). 当begin > end 时,返回空(注意不是

Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 终于开始动态规划的题目了.这道题目需要耐心挖掘其中的规律,首先根不一样的二叉搜索树肯定是不同的搜索树.

uva 122 trees on the level——yhx

题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes. In a level

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}&