zoj zju 2992 Monkey Vines 二叉树

Monkey Vines

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and juniper bugs, which happen to be the culinary delight of brown monkeys.

Reaching the canopy of these trees requires the monkeys to perform careful navigation through the tall tree??s fragile vine system. These vines operate like a see-saw: an unbalancing of weight at any vine junction would snap the vine from the tree, and the
monkeys would plummet to the ground below. The monkeys have figured out that if they work together to keep the vines properly balanced, they can all feast on the figs and juniper bugs in the canopy of the trees.

A vine junction supports exactly two sub-vines, each of which must contain the same number of monkeys, or else the vine will break, leaving a pile of dead monkeys on the jungle ground. For purposes of this problem, a vine junction is denoted by a pair of matching
square brackets [ ], which may contain nested information about junctions further down its sub-vines. The nesting of vines will go no further than 25 levels deep.

You will write a program that calculates the minimum number of monkeys required to balance a particular vine configuration. There is always at least one monkey needed, and, multiple monkeys may hang from the same vine.

Input

The first line of input contains a single integer N, (1 <= N <= 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a vine configuration consisting of a string of [ and ] characters as described above. The length of the string of [ and ] will be greater than or equal to zero, and less than or equal to 150.

Output

For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the minimum number of monkeys required to reach the canopy successfully. Assume that all the hanging
vines are reachable from the jungle floor, and that all monkeys jump on the vines at the same time.

Sample Input

3

[]

[[][[]]]

Sample Output

1 2

2 1

3 8

Note: The second line of sample input is a blank line.

题意:有一棵完整的二叉树,下面有一群猴子。要求出最小的猴子数量,来使这棵二叉树是平衡的。平衡就是指同一个父节点下面两个儿子节点上面呆的猴子数量必须相同。还有这个树上至少要有一只猴子。

思路:画几个图就可以看出,二叉树上如果想平衡,要看最深的节点的深度。2^dep 就是最少需要多少只猴子来平衡这棵树。但是输入的时候,所给的是用括号来表示这棵树。左括号是表示向下延伸,右括号表示回到父亲节点。注意这棵二叉树的根的上面还有一个根。所以要从图片中的黑色实心半球开始。图中红色数字表示那串左右括号,在这棵树上走的先后顺序。所以求深度,只用一次线性扫描就行了,左括号+,右括号-。

#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{
	int t;
	int cas=1;
	int n;
	char str[500];
	int op;
	int ans,dep;
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		gets(str);
		ans=0;
		dep=0;
		for(int i=0;str[i];i++)
		{
			if(str[i]=='[')
				dep++;
			else
				dep--;
			if(dep>ans)
				ans=dep;
		}
		int er=1;
		for(int i=0;i<ans;i++)
		{
			er*=2;
		}
		printf("%d %d\n",cas++,er);

	}

	return 0;
}
时间: 2024-07-31 09:18:12

zoj zju 2992 Monkey Vines 二叉树的相关文章

HDU 1990 &amp; ZOJ 2992 Monkey Vines(数学啊)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1990 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1991 Problem Description Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and junipe

zoj zju 2994 Tiling a Grid With Dominoes 状压dp

Tiling a Grid With Dominoes Time Limit: 2 Seconds      Memory Limit: 65536 KB We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five differen

zoj zju 2989 Encoding 字符串处理

Encoding Time Limit: 2 Seconds      Memory Limit: 65536 KB Chip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in

zoj zju 2990 Decoding 字符串处理

Decoding Time Limit: 2 Seconds      Memory Limit: 65536 KB Chip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in

zoj zju 2991 Flipping Burned Pancakes

Flipping Burned Pancakes Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As a result, one side of a stack of pancakes is often

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

ZOJ 2334 HDU 1512 Monkey King

题意: 猴子们打架  认识的猴子不会打架  两只猴子打完以后就认识了  A认识B B认识C A也认识C  每次打架由两伙猴子进行  分别选出自己的最高战斗力  在战斗之后两只猴子战斗力减半  给出m次打架  输出打架后这一伙猴子里的最强战斗力 思路: 判断两只猴子是不是一伙的  用到并查集 快速找出一伙猴子中的最强战斗力用到堆  但打完架两伙猴子合并时堆需要nlogn复杂度  因此用左偏树代替堆 代码: #include<cstdio> #include<cstring> #inc

zoj 2334 Monkey King

Monkey King ZOJ - 2334 题目大意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害.如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认识了,不打不相识嘛.现在给m组询问,如果2只猴子相互认识,输出-1,否则他们各自找自己认识的最牛叉的猴子单挑,求挑完后这拨猴子力量最大值. /* 左偏树模板题,包括合并,删除操作 */ #include<iostream> #

zoj 1944 Tree Recovery (二叉树)

Tree Recovery Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one