• uva10562Undraw the Trees


    根据样例可以明显看出题意。。比较坑的一点注意节点可以是任意字符,不一定是字母,还要判断是否有换行符。 另外空节点要独立判断

    题目:

    Problem D
    Undraw the Trees
    Input: Standard Input

    Output: Standard Output

    Time Limit: 2 Seconds

     

    Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don't know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor's works were found in a 3.5" floppy disk left inside the drive.

    The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek -- we leave this trivial task for you. Convert professor's trees to our trees.

    Professor's Trees

    The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 <= T <= 20) drawn in the file. Then you would have T trees, each ending with a single hash ('#') mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols '-', '|', ' ' (space) and '#'. Every node that has children has a '|' symbol drawn just below itself. And in the next line there would be a series of '-' marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.

    Our Trees

    Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.

     

    Sample Professor’s Trees      Corresponding Our Trees

    2

        A

        |

    --------

    B  C   D

       |   |

     ----- -

     E   F G

    #

    e

    |

    ----

    f g

    #

     

    (A(B()C(E()F())D(G())))

    (e(f()g()))

     


    Problemsetter: Monirul Hasan, Member of Elite Problemsetters' Panel

    代码:

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cctype>
     5 #include <memory.h>
     6 using namespace std;
     7 
     8 const int maxn = 300;
     9 char tree[30][maxn];
    10 
    11 
    12 int dfs(int left,int right,int n)
    13 {
    14     for(int i=left;i<right;i++)
    15     {
    16         if(tree[n][i]!=' ' &&tree[n][i]!='')
    17         {
    18             if(tree[n+1][i]!='|'){cout<<tree[n][i]<<"()";}
    19             else
    20             {
    21                 int up=i,down=i;
    22                 while(tree[n+2][up]=='-')up--;
    23                 while(tree[n+2][down]=='-')down++;
    24                 cout<<tree[n][i];
    25                 cout<<"(";
    26                 dfs(up+1,down,n+3);
    27                 cout<<")";
    28             }
    29         }
    30     }
    31     return 0;
    32 
    33 
    34 }
    35 
    36 
    37 int main()
    38 {
    39 
    40     freopen("in.txt","r",stdin);
    41     int tst;
    42     cin>>tst;
    43     getchar();
    44     while(tst--)
    45     {
    46         int p=0;
    47         while(gets(tree[p++]))
    48         {
    49             if(tree[p-1][0]=='#')break;
    50         }
    51         if(p-1==0)
    52         {
    53             cout<<"()"<<endl;
    54         }
    55         else
    56         {
    57             cout<<"(";
    58             dfs(0,maxn,0);
    59             cout<<")"<<endl;
    60         }
    61         memset(tree,0,sizeof(tree));
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    java maven 操作 收集的一些命令
    Java多线程,线程交替执行
    java类同时引用父类和接口的成员变量,需要指明是父类的还是接口的
    artTemplate 模板使用
    Dubbo 只注册,只订阅
    Tomcat灵活配置多项目,多端口,多域名,多虚拟目录
    CentOS7.2 安装nginx-1.10.3
    CentOS7.2 安装zookeeper3.4.9
    CentOS7.2 安装Redis3.2.8
    CentOS7.2 切换成iptables规则
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3473763.html
Copyright © 2020-2023  润新知