acm水题 二叉树模拟 hdu5444,能想到的测试数据都测了还是WA,求大神

问题描述

acm水题 二叉树模拟 hdu5444,能想到的测试数据都测了还是WA,求大神

1)我的代码(题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5444,也有复制内容在代码下面)

 #include <iostream>
#include <string.h>
using namespace std;
const int maxn=1010;
char record[maxn];
int sum=0;
struct tree{
    int left;
    int right;
}tree_1[maxn];
void build_tree(int root,int num)
{
    if(root>num&&!tree_1[root].right){
        tree_1[root].right=num;
    }
    if(root<num&&!tree_1[root].left){
        tree_1[root].left=num;
    }
    if(root>num){
        build_tree(tree_1[root].right,num);
    }
    if(root<num){
        build_tree(tree_1[root].left,num);
    }
}
void dfs_tree(int root,int num)
{

    if(num==root&&!sum){
        sum=1;
        return ;
    }
    if(num<root&&tree_1[root].right){
        record[sum++]='E';
        dfs_tree(tree_1[root].right,num);
    }
    else if(num>root&&tree_1[root].left){
        record[sum++]='W';
        dfs_tree(tree_1[root].left,num);
    }
}
int main()
{
    int kase;cin>>kase;
    while(kase--){
        int n;cin>>n;
        int root;cin>>root;
        n--;
        while(n--){
            int room_num;cin>>room_num;
            build_tree(root,room_num);
        }

        int m;cin>>m;
        record[0]='';
        while(m--){
            int bk_num;cin>>bk_num;
            dfs_tree(root,bk_num);
            for(int i=0;i<sum;i++){
                cout<<record[i];
            }
            cout<<endl;
            memset(record,0,sizeof(record));
            record[0]='';
            sum=0;
        }
        memset(tree_1,0,sizeof(tree_1));

    }
}

2)
Problem Description
Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may 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 is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two 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 that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.

Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.

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

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

Input
First you are given an integer T(T≤10) indicating the number of test cases.

For each test case, there is a number n(n≤1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,...,an where a1,...,an∈{1,...,n}.

On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,...,xq indicating the destination room number of each mail.

Output
For each query, output a sequence of move (E or W) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W 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 always starts from the root regardless of the room he had just visited.

Sample Input
2
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1

Sample Output
E

WE
EEEEE

解决方案

ac了,在最后那里的for循环中加了一个if,如果record[0]==''的话不输出直接break,不然输出的不是空行,是‘a’

 #include <iostream>
#include <string.h>
using namespace std;
const int maxn=1010;
char record[maxn];
int sum=0;
struct tree{
    int left;
    int right;
}tree_1[maxn];
void build_tree(int root,int num)
{
    if(root>num&&!tree_1[root].right){
        tree_1[root].right=num;
    }
    if(root<num&&!tree_1[root].left){
        tree_1[root].left=num;
    }
    if(root>num){
        build_tree(tree_1[root].right,num);
    }
    if(root<num){
        build_tree(tree_1[root].left,num);
    }
}
void dfs_tree(int root,int num)
{

    if(num==root&&!sum){
        sum=1;
        return ;
    }
    if(num<root&&tree_1[root].right){
        record[sum++]='E';
        dfs_tree(tree_1[root].right,num);
    }
    else if(num>root&&tree_1[root].left){
        record[sum++]='W';
        dfs_tree(tree_1[root].left,num);
    }
}
int main()
{
    int kase;cin>>kase;
    while(kase--){
        int n;cin>>n;
        int root;cin>>root;
        n--;
        while(n--){
            int room_num;cin>>room_num;
            build_tree(root,room_num);
        }

        int m;cin>>m;
        record[0]='';
        while(m--){
            int bk_num;cin>>bk_num;
            dfs_tree(root,bk_num);
            for(int i=0;i<sum;i++){
                if(record[0]==''){
                    break;
                }
                cout<<record[i];
            }
            cout<<endl;
            memset(record,0,sizeof(record));
            record[0]='';
            sum=0;
        }
        memset(tree_1,0,sizeof(tree_1));

    }
}

解决方案二:

http://blog.csdn.net/ms961516792/article/details/48435817

时间: 2024-08-03 18:38:54

acm水题 二叉树模拟 hdu5444,能想到的测试数据都测了还是WA,求大神的相关文章

遍历-数据结构问题。二叉树,程序写了编译没错,但没办法运行。求大神看下。

问题描述 数据结构问题.二叉树,程序写了编译没错,但没办法运行.求大神看下. #include #define MAXLEN 100 using namespace std; typedef char elementType; typedef struct lBnode {elementType data; struct lBnode *lchild,*rchild; }Binode,*Bitree; void create(Bitree &T) //创建二叉链表 {char ch; cin>

ubuntu-Ubuntu下用信号量模拟的十字路口,可以在qt中实现吗,求大神解决以下是代码

问题描述 Ubuntu下用信号量模拟的十字路口,可以在qt中实现吗,求大神解决以下是代码 sn.c南北方向 #include #include #include #include #include #include #include #include int sem_id; void init(){ key_t key;int ret; unsigned short sem_array[2]; union semun { int val; struct semid_ds *buf; unsign

功率谱估计-谁会这俩道自适应滤波的题啊,时域离散随机信号处理方面的,急急急,求大神

问题描述 谁会这俩道自适应滤波的题啊,时域离散随机信号处理方面的,急急急,求大神 跪求大神啊!有关于数字信号处理中的时域离散随机信号处理的内容,真的很急!大神!大神! 解决方案 题就是这两张图上的,求大神给出上帝之手!

acm背包问题求大神赐教

问题描述 acm背包问题求大神赐教 for(i = 1; i<=n; i++) { for(j = v; j>=c[i]; j--)//在这里,背包放入物品后,容量不断的减少,直到再也放不进了 { f[i][v]=max(f[i-1][v],f[i-1][v-c[i]]+w[i]); } } 请问 第一个循环 i =1 之后 第二个循环 一直在放编号为1的石头啊?这两个循环都是干嘛的?那个 f[i][v] 数组 从00到 iv 都变成什么样了?求大神赐教 解决方案 直接说题目是什么你这么问,没

字符-一道acm水题 all in all 一直找不出错误 求大神解答

问题描述 一道acm水题 all in all 一直找不出错误 求大神解答 描述字符串s和t均由字母组成,若在t中除去一些字母能够得到s,我们就说s是t的一个子串.比如abc就是acbefc的子串(acbefc去掉第二.第四.第五个字符后就得到abc)输入有若干组输入数据,每组一行,分别为字符串s和t,s与t之间用空格隔开输出对于一组s与t,若s是t的子串,则输出Yes,否则输出No 样例输入sequence subsequence abc acb VERDI vivaVittorioEmanu

acm 算法 暴力-acm水题求解,关于贪心算法

问题描述 acm水题求解,关于贪心算法 #include #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop / ???? int main(int argc, char* argv) { ???? int M,N; ???? while(cin>>M&g

c++问题-在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里。不胜感激!!!

问题描述 在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里.不胜感激!!! #include #include using namespace std; int main() { int T; int k,t=0; int i, j, n1, n2; char a[1010], b[1010], c[1015]; string d[20], e[20], f[20]; cin>>T; for(k=1; k<=T; k++) { cin>>a>>b; d[

acm-一道ACM的题求大神们解答,C语言,谢谢!

问题描述 一道ACM的题求大神们解答,C语言,谢谢! [题目描述] FFF团成员自带这样一个属性:凭空变出火把与汽油,两者配合起来才能让FFF之火duang的一下烧起来,但是不同的火把与不同的汽油配合产生的火焰是不同的,现在有n种火把与n种汽油,已知每一种火把与每一种汽油配合时产生的火焰的旺盛程度,现在求怎样使得火把与汽油一一配对,产生最旺盛的火焰. [输入] 第一行为一个整数T,表示有T组数据 每组数据第一行为一个正整数n(2≤n≤30) 第二行开始一共有n行,每行为n个正整数,第i行第j个数

c++-这个数据结构题……求大神

问题描述 这个数据结构题--求大神 有n个字符的字符串,判断字符串是否回文. 如: abcba和abccba都是回文:实验要求: 1.将输入的字符串放在单链表栈中: 2. 利用栈的入栈和出栈完成是否回文判断: 3. 应提供栈的基本操作,如栈的初始化.销毁.判断是否栈空等: 怎么用c++实现呢? 解决方案 假设你已经实现了堆栈(如果这个有困难,可以Google例子程序),那么实现的思路是: for (int i = 0; i < (strlen(s) + 1) / 2; i++) stack.Pu