UVa 712-s-trees

Source: Internet
Author: User

Title: give you a completely binary tree, and give you a xi1~xin-1 sequence, the first layer from the root to the leaves;

Each layer corresponds to an XI, the leaf is 0 or 1, now gives you some path, the output path corresponds to the value of the leaf;

The path is 01 strings, from the beginning to go down, 0 for the left subtree, 1 for the right subtree, corresponding to the order of x1~xn-1.

Analysis: Simulation. Direct Ann find the path to go over, because it is two fork tree, just corresponds to the binary number.

Note: The order of the paths is adjusted to the same as the tree in the calculation.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;int x[10],f[10];int leaf[200];int main () {int  n,k,t = 1;char buf [200];while (~SCANF ("%d", &n) && N) {for (int i = 0; i < n; + + i) {scanf ("%s", buf); X[i] = Atoi (&buf[1] )-1;} scanf ("%s", buf); int m = 1<<n;for (int i = 0; i < m; + + i) leaf[i] = buf[i]-' 0 ';p rintf ("S-tree #%d:\n", T + +); scan F ("%d", &k), for (int i = 0; i < K; + + i) {scanf ("%s", buf), for (int j = 0; J < N; + + j) F[x[j]] = buf[j]-' 0 '; in T v = 0;for (int j = 0; J < N; + + j) v = 2*v+f[x[j]];p rintf ("%d", Leaf[v]);} printf ("\ n");}    return 0;}


UVa 712-s-trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.