uva10562

Problem D
Undraw the Trees
Input:
StandardInput

Output: Standard Output

Time Limit: 2 Seconds

 

Professor Homer has been reported missing. We suspect thathis recent research works might have had something to with this. But we reallydon't know much about what he was working on! The detectives tried to hack intohis computer, but after hours of failed efforts they realized that theprofessor had been lot more intelligent than them. If only they could realizethat the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprisedwhen 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 professorhad drawn many trees with ASCIIcharacters. Before we can proceed to the next level of investigation we wouldlike to match the trees drawn with the ones that we have in our database. Nowyou are the computer geek -- we leave this trivial task for you. Convertprofessor's trees to our trees.

Professor'sTrees

The first line of the input file (which you can assume comesfrom 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 theline. All the trees drawn here are drawn vertically in top down fashion. Thelabels 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 therewould be a series of '-' marks atleast as long as to cover all the immediate children. The sample input sectionwill hopefully clarify your doubts if there is any. No tree drawn here requiresmore than 200 lines, and none ofthem has more than 200 characters inone line.

OurTrees

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

 

 

Sample Professor’sTrees      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

悲催的题。忘了对M初始化,结果老wrong。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char M[240][240],S[240];
int o=0,k;
void search(int j,int p)
{
    for(int i=p; M[j-1][i]=='-'; i++)
    {
        if(M[j][i]&&M[j][i]!=' '&&M[j][i]!='|'&&M[j][i]!='-'&&M[j][i]!='#')
        {
            int kk=i;
            S[o++]=M[j][i];
            S[o++]='(';
            if(j+3<k&&M[j+1][kk]=='|')
            {
                int ss;
                for(ss=kk; ss>=0; ss--)
                    if(M[j+2][ss]!='-')break;
                if(M[j+2][ss]!='-')ss++;
                search(j+3,ss);
            }
            S[o++]=')';
        }
    }
}
int main()
{
    // freopen("1.txt","r",stdin);
    int n;
    cin>>n;
    getchar();
    for(int i=0; i<n; i++)
    {
        memset(M,0,sizeof(M));
        k=0;
        while(gets(M[k])!=NULL)
        {
            if(M[k][0]=='#')break;
            else k++;
        }
        o=0;
        S[o++]='(';
        for(int s=0; M[0][s]; s++)
        {
            if(M[0][s]!=' '&&M[0][s]!='|'&&M[0][s]!='-'&&M[0][s]!='#')
            {
                S[o++]=M[0][s];
                S[o++]='(';
                if(k>3&&M[1][s]=='|')
                {
                    int ss;
                    for(ss=s; ss>=0; ss--)if(M[2][ss]!='-')break;
                    if(M[2][ss]!='-')ss++;
                    search(3,ss);
                }
                S[o++]=')';
            }
        }
        S[o++]=')';
        S[o]='\0';
        cout<<S<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值