UVA 10562 Undraw the Trees

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

 




题意:就是看图。。写树。。。

思路:用二维数组存下这个图形 ,找-----------的串。然后如果他的下一层是字母。就输出,并且判断字母的下一层是不是‘|’ 是的话就跳到下一层进行搜索。。。如果遇到字母下一层不是‘|’ 就输出(),,遍历过的-----全部转换为‘空格‘。

大概就是这样了。。。不过要注意的是,图中不一定是字母构成结点。只要不是'|' '\0' '空格’ '-'的字符都可以构成。。

这个害我WA了次。。

还有空树也是要输出()的。。。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int t;
int tt;
char tu[205][205];

void dfs(int cen)
{
	int i;
	int len = strlen(tu[cen]);
	int ii = 0;
	while (tu[cen][ii] != '-')
	{
		ii ++;
	}
	for (i = ii; i < len; i ++)
	{
		if (tu[cen][i] != '-')
			return;
		if (tu[cen + 1][i] != '\0' && tu[cen + 1][i] != '-' && tu[cen + 1][i] != '|' && tu[cen + 1][i] != '#' && tu[cen + 1][i] != ' ' && tu[cen][i] == '-')
		{
			tu[cen][i] = ' ';
			printf("%c", tu[cen + 1][i]);
			if (tu[cen + 2][i] == '|')
			{
				printf("(");
				dfs(cen + 3);
				printf(")");
			}
			else
				printf("()");
		}
		if (tu[cen][i] == '-')
			tu[cen][i] = ' ';
	}
}
int main()
{
	scanf("%d", &t);
	getchar();
	while (t --)
	{
		int i;
		tt = 1;
		memset(tu, 0, sizeof(tu));
		for (i = 0; i < 204; i ++)
			tu[0][i] = '-';
		while (gets(tu[tt]) != NULL && tu[tt][0] != '#')
		{
			tt ++;
		}
		printf("(");
		dfs(0);
		printf(")");
		printf("\n");
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值