UVa 712 S-Tree _uva

Source: Internet
Author: User

There is a complete binary tree, each layer of elements have the same variable, from top to bottom respectively for the x1,x2,... The last leaf node will have 0 or 1 of the assignment, the input given. Then if Xi takes a value of 0 o'clock, go to the left subtree, or go to the right subtree until you reach the leaf node and get a value.

Train of thought: because it is a complete binary tree, you can use sequential storage, array implementation can be. In addition, just save the value of the leaf node. For a node k, the left child is 2k, the right child is 2k+1. Because the height of the maximum is 7, there is a maximum of x7, so you can directly take the second position of the array, that is subscript 1, to obtain XK K.

Note: There is a blank line after each test sample, WA once ~

Two-dimensional arrays as parameters: one is char (*PT) [4]; The second is Char pt[][5] if written as Char *pt[5, the error is prompted: cannot convert ' char (*) [5] ' to ' char** ' for argument ' 3 ' to ' void process (char*, int, char**) ' means that the argument is char (*) [5], and the formal parameter is char** and does not match.

[] has a higher precedence than *,char (*PT) [4] that represents a pointer to an array, a pointer to an array of 4 char values, a char *pt[5] that represents an array of pointers, a char-pointer array, and 5 elements.

Code:

#include <stdio.h>
#include <string.h>
#define MAXN

-void process (char *vva,int Len,char VARORD[][5]);

int TREE[MAXN];

int main ()
{
 int cnt=1;
 int n;
 while (scanf ("%d", &n) ==1 && N)
 {
  char varord[8][5];
  for (int i=0;i<n;++i)
   scanf ("%s", Varord[i]);
  GetChar ();
  for (int i=0;i< (1<<n); ++i)
  {
   char c=getchar ();//printf ("%c", c)
   ; tree[(1<<n) +i]=c-' 0 ';       
  }
  for (int i=0;i< (1<<n); ++i)
  //printf ("%d", tree[i+ (1<<n));
  int m;
  scanf ("%d", &m);
  printf ("S-tree #%d:\n", cnt++);
  for (int i=0;i<m;++i)
  {
   char vva[10];
   scanf ("%s", VVA);
   Process (Vva,strlen (VVA), Varord);       
  }
  printf ("\ n \ nthe");
 }
 return 0;   
}

void process (char *vva,int Len,char varord[][5])
{
 int k=1;
 for (int i=0;i<len;++i)
 {
  int t=varord[i][1]-' 0 ';//Because the maximum is X7, you can subtract the character 0 directly. Note that the subscript is 1, not 2.
  if (vva[t-1]== ' 0 ') k=2*k;
  else k=2*k+1;       
 }
 printf ("%d", tree[k]);    



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.