|
712-S-trees |
2807 |
51.34% |
1408 |
85.37% |
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 104 & page = show_problem & problem = 653
Question type: data structure, binary tree
Sample input:
3x1 x2 x30000011140000101111103x3 x1 x20001001140000101111100
Sample output:
S-Tree #1:0011S-Tree #2:0011
Question:
For a sequence set VVI {x1, x2, X3 ,...., xn}. In VVI, if it is not 0, it is 1 and then there is a tree with N layers. Each layer has the same number at the same layer. This number is taken from VVI, the input will show which layer is in VVI
The last layer is the leaf node, which is a string of given numbers.
Starting from the following node, if the node is 0, it will go to the left son direction, if it is 1, it will go to the right son direction. The number is output at the leaf node of the last layer.
Solution:
This question should be a very watery question in this binary tree topic. You do not need to create a result. If it is 1, the current node is * 2 + 1. If it is 0, it is multiplied. Finally, a number is obtained. This number is subtracted from the sum of the nodes at the front layer, and then the corresponding result can be output.
For more information, see the code.
#include<iostream>#include<cstdio>#include<vector>using namespace std;int n,m;char termi[200];char vva[200];vector<int>vtOrder;inline void Solve(){ char temp[10]; int val; vtOrder.clear(); for(int i=0; i<n; ++i){ scanf("%s", temp); sscanf(temp+1, "%d", &val); vtOrder.push_back(val); } scanf("%s", termi); scanf("%d", &m); while(m--){ scanf("%s", vva+1); int pos=1; for(int i=0; i<vtOrder.size(); ++i){ if(vva[vtOrder[i]]=='0') pos = pos*2; else pos = pos*2+1; } printf("%c", termi[pos-(1<<n)]); } printf("\n");}int main(){ freopen("input.txt", "r", stdin); int cas=1; while(scanf("%d", &n) && n){ printf("S-Tree #%d:\n", cas++); Solve(); printf("\n"); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double