UVA 712 S-trees

Source: Internet
Author: User

Original title:
The topic is too long, not posted online.
topic:
give you a number n, tell you that this is an n-level complete binary tree, each layer corresponds to a letter Xi, and then give you a string to represent the value of the leaf point. Next I'll give you a number m, with M query string, only 0 and 1 in each string, where 0 means go from root to Zuozi, 1 is right. Finally ask you m query to the value of the leaf node to make up a string is how much.

#include <iostream> #include <algorithm> #include <map> #include <string> #include <cstring > #include <sstream> #include <cstdio> #include <vector> #include <cmath> #include <stack > #include <queue> #include <iomanip> #include <set> #include <fstream> #include <limits.h
> Using namespace std;

FStream output,input;
int tree[1024];
int n,m;
Vector<int> ans;
    int main () {Ios::sync_with_stdio (false);
    string S;
    int tmp,x=1;
        while (cin>>n,n) {memset (tree,-1,sizeof (-1));
        Ans.clear ();
        for (int i=0;i<n;i++) cin>>s;
        cin>>s;
        int k= (int) pow (2,n);
        for (int i=0;i<s.size (); i++) tree[k+i]=s[i]-' 0 ';
        cin>>m;
            for (int i=0;i<m;i++) {cin>>s;
            Tmp=1;
                for (int j=0;j<s.size (); j + +) {if (s[j]== ' 0 ')    tmp*=2;
            else tmp=tmp*2+1;
        } ans.push_back (Tree[tmp]);
        } cout<< "S-tree #" <<x++<< ":" <<endl;
        for (int i=0;i<ans.size (); i++) cout<<ans[i];
        cout<<endl;
    cout<<endl;
} return 0;


 }

Answer:
When I just read it, I was really scared and I thought it was going to be a lot of effort. After reading the feeling this problem is a waste problem, do not build, directly with the array storage can. The input of each XI value is not used, the last traversal of the query string if it is 0 means to left to let tmp*2, go to the right to let tmp*2+1d, finally to the leaf node, the value can be removed.

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.