ACM/ICPC Asia Regional Changchun Online HDU 5444 elven Postman "binary sorting tree achievement and Traversal lookup"

Source: Internet
Author: User

Elven Postman

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 591 Accepted Submission (s): 329


Problem descriptionelves is very peculiar creatures. As we all know, they can live for a very long time and their magical prowess is not something to be taken lightly. Also, they live on trees. However, there is something on them you are not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more "traditional" methods.

So, as a elven postman, it's crucial to understand how to deliver the mail to the correct the tree. The Elven tree always branches to no more than and paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only then, when numbering the rooms, they always number the "the" number from the East-most position to the west. For rooms in the east is usually more preferable and more expensive due to they have the privilege to see the sunrise, Which matters a lot in elven culture.

Anyways, the elves usually wrote down all the rooms with a sequence at the root of the "the Tree So" the postman may know how To deliver the mail. The sequence is written as follows, it'll go straight to visit the East-most and write down every the IT encounter Ed along the The. After the first reached, it'll then go to the next unvisited east-most, writing down every unvisited n the "as well" until all rooms is visited.

Your task is to determine how to reach a certain-given the sequence written on the root.

For instance, the sequence 2, 1, 4, 3 would is written on the root of the following tree.

Inputfirst you is given an integerT(t≤ten) indicating the number of test cases.

For each test case, there is a numbern(n≤) On a line representing the number of rooms in this tree.NIntegers representing the sequence written at the root follow, respectivelyA1,.. . ,an whereA1,...,AN{1,. , N} .

On the next line, there is a numberQRepresenting the number of mails to be sent. After that, there'll beQIntegersX1,.. q indicating the destination, number of each mail.

Outputfor each query, output a sequence of move (EOrWThe postman needs to do to deliver the mail. For thatEmeans that the postman should move up the eastern branch andW the western one. If the destination is on the root, just output a blank line would suffice.

Note that for simplicity, we assume the postman all the starts from the root regardless of the the the-had just visited.

Sample INPUT2//Representative has 2 sets of test data 4//4 data need to be inserted into two fork Tree 2 1 4 33//For 3 Inquiries 1 2 3 66 5 4 3 2 111

Sample outpute //output is empty   because 2 is the root node   does not move Weeeeeesource acm/icpc Asia regional Changchun Online   Direct copy of the topic after the problem: the original question please see http://acm.hdu.edu.cn/showproblem.php?pid=5444  analysis: In order to review the data structure, the code of the problem pasted out, but also easy to review later. This is a simple data structure problem, can be seen as the establishment of a inverted two-fork sorting tree. Then we go to find the elements on the tree, W and E for the left and right moving direction and finally reach the target number.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <    Queue> #include <algorithm>using namespace std;typedef struct node{int data;    struct node *ll; struct node *rr;}        Binode, *bitree;void creat_sort_bitree (bitree &root, int key) {if (root==null) {root=new binode;//a type        root->data=key;        root->ll=null;        root->rr=null;        return;//insert to two fork sort tree successfully returned} else{if (Key < Root->data) {Creat_sort_bitree (root->ll, key);        }else{Creat_sort_bitree (ROOT-&GT;RR, key);    }}}queue<char>q;void Get_aim (bitree &root, int key) {if (Root->data = = key) {return;            }else{if (Root->data > key) {q.push (' E ');        Get_aim (Root->ll, key);            } else{Q.push (' W ');        Get_aim (ROOT-&GT;RR, key);    }}}int Main () {int TG; scanf ("%d", &AMP;TG); int N, I, J, DD, M;    Bitree Root; while (tg--) {root=null;//Initialize root must be empty!        This root value is scanf ("%d", &n) that does not change after the root node has been determined;            while (n--) {scanf ("%d", &AMP;DD);        Creat_sort_bitree (Root, DD);        } scanf ("%d", &m);            while (m--) {scanf ("%d", &dd),//In the binary sort tree will find out while (!q.empty ())) Q.pop ();//global variable queue remember to empty after using            Get_aim (Root, DD);            while (!q.empty ()) {printf ("%c", Q.front ()); Q.pop ();        }printf ("\ n"); }} return 0;}

ACM/ICPC Asia Regional Changchun Online HDU 5444 elven Postman "binary sorting tree achievement and Traversal lookup"

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.