10562 - Undraw the Trees

Professor Homerhas been reported missing. We suspect that his recent research works might havehad something to with this. But we really don't know much about what he wasworking on! The detectives tried to hack into his computer, but after hours of failedefforts they realized that the professor had been lot more intelligent thanthem. If only they could realize that the professor must have been absentminded they could get the clue right away. We at the crime lab were not atall surprised when the professor's works were found in a 3.5"floppy disk left inside the drive.

The disk containedonly 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 matchthe trees drawn with the ones that we have in our database. Now you are thecomputer geek -- we leave this trivial task for you. Convert professor's treesto our trees.

Professor'sTrees

The first line ofthe input file (which you can assume comes from standard input) contains thenumber 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 drawnvertically in top down fashion. The labels of each of node can be any printablecharacter except for the symbols '-''|'' ' (space)and '#'. Every node that has children has a '|' symbol drawnjust below itself. And in the next line there would be a series of '-' marksat least as long as to cover all the immediate children. The sample inputsection will hopefully clarify your doubts if there is any. No tree drawn hererequires more than 200 lines, and none of them has morethan 200 characters in one line.

Our Trees

Our trees aredrawn with parenthesis and the node labels. Every sub tree startswith an opening parenthesis and ends with a closing parenthesis; inside theparenthesis the node labels are listed. The sub trees are always listed fromleft to right. In our database each tree is written as a single string in oneline, and they do not contain any character except for the node labels and theparenthesis pair. The node labels can be repeated if the original tree had suchrepetitions.

 

 

SampleProfessor’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()))

 

代码:

题意:把画得挺好看的多叉树转化为括号表示法

算法:直接在二维字符数组里递归。注意空树,并且结点标号可以是任意可打印字符

#include<cstdio>

#include<cctype>

#include<cstring>

using namespace std;

 

const int maxn = 200 + 10;

int n;

char buf[maxn][maxn];

 

递归遍历并且输出以字符buf[r][c]为根的树

void dfs(int r, int c)

{

   printf("%c(", buf[r][c]);

   if(r+1 < n && buf[r+1][c] == '|')  有子树

    {

       int i = c;

       while(i-1 >= 0 && buf[r+2][i-1] == '-')

       {

           i--; 找"----"的左边界

       }

       while(buf[r+2][i] == '-' && buf[r+3][i] != '\0')

       {

           if(!isspace(buf[r+3][i]))

           {

                dfs(r+3, i);  fgets读入的'\n'也满足isspace()

           }

           i++;

       }

    }

   printf(")");

}

 

void solve()

{

    n= 0;

   for(;;)

    {

       fgets(buf[n], maxn, stdin);

       if(buf[n][0] == '#')

       {

           break;

       }

       else

       {

           n++;

       }

    }

   printf("(");

   if(n)

    {

       for(int i = 0; i < strlen(buf[0]); i++)

       {

           if(buf[0][i] != ' ')

           {

                dfs(0, i);

                break;

           }

       }

    }

   printf(")\n");

}

 

int main()

{

    int T;

   fgets(buf[0], maxn, stdin);

   sscanf(buf[0], "%d", &T);

   while(T--)

    {

       solve();

    }

   return 0;

}

 

自己写的WA的没找到错误的代码:

#include<iostream>

#include<string>

using namespacestd;

 

string s[220];

int n;

 

void dfs(int r,intc);

 

int main()

{

    int test;

    cin>>test;

    cin.get();

    while(test--)

    {

        n=0;

       while(getline(cin,s[n])&&s[n][0]!='#')

        {

            n++;

        }

        cout<<"(";

        for(int i=0;i<s[0].size();i++)

        {

            if(s[0][i]!=' ')

            {

                dfs(0,i);

                break;

            }

        }

        cout<<")\n";

    }

    return 0;

}

 

void dfs(int r,intc)

{

    cout<<s[r][c]<<'(';

    if(r+1<n&&s[r+1][c]=='|')

    {

        int i=c;

       while(i>=1&&s[r+2][i-1]=='-')

        {

            i--;

        }

       while(s[r+2][i]=='-'&&i<s[r+2].size())

        {

           if(i<s[r+3].size()&&s[r+3][i]!=' ')

            {

                dfs(r+3,i);

            }

            i++;

        }

    }

    cout<<")";

}

 

自己写的:

#include<iostream>

#include<string>

using namespacestd;

 

string s[220];

int n;

 

void dfs(int r,intc);

 

int main()

{

    int test;

    cin>>test;

    cin.get();

    while(test--)

    {

        for(int i=0;i<220;i++)

        {

            s[i]="";

        }

        int i=-1;

       while(getline(cin,s[++i])&&s[i]!="#");

        n=i;

        cout<<"(";

        for(int i=0;i<s[0].size();i++)

        {

            if(s[0][i]!=' ')

            {

                dfs(0,i);

            }

        }

        cout<<")\n";

    }

 

    return 0;

}

 

void dfs(int r,intc)

{

    cout<<s[r][c]<<"(";

    if(r+1<n&&s[r+1][c]=='|')

    {

        int i=c;

       while(i>=1&&s[r+2][i-1]=='-')

        {

            i--;

        }

       for(;s[r+2][i]=='-';i++)

        {

           if(i<s[r+3].size()&&s[r+3][i]!=' ')

            {

                dfs(r+3,i);

            }

        }

    }

    cout<<")";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值