UVa 12166(字符型二叉树的dfs)

一.题目

[PDF Link]

A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium. It consists of a number of rods, from which weighted objects or further rods hang. The objects
hanging from the rods balance each other, so that the rods remain more or less horizontal. Each rod hangs from only one string, which gives it freedom to rotate about the string.

We consider mobiles where each rod is attached to its string exactly in the middle, as in the figure underneath. You are given such a configuration, but the weights on the ends are chosen incorrectly, so that the mobile is not in equilibrium. Since that‘s
not aesthetically pleasing, you decide to change some of the weights.

What is the minimum number of weights that you must change in order to bring the mobile to equilibrium? You may substitute any weight by any (possibly non-integer) weight. For the mobile shown in the figure, equilibrium can be reached by changing the middle
weight from 7 to 3, so only 1 weight needs to changed.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with the structure of the mobile, which is a  recursively defined expression of the form:

      <expr> ::= <weight> | "[" <expr> "," <expr> "]"

    with <weight> a positive integer smaller than  109 indicating a weight and 
    [<expr>,<expr>]  indicating a rod with the two expressions at the ends of the rod.  The total number of rods in the chain from a weight to the top of  the mobile will be at most 16.

Output

Per testcase:

  • One line with the minimum number of weights that have to be changed.

Sample Input

3
[[3,7],6]
40
[[2,3],[4,5]]

Sample Output

103

二.分析与实现

本题很有意思,首先要分析发现,通过“平衡”这个关系,一个叶子就可以决定其他所有叶子的大小,进一步思考,也就是说一个节点就决定了整颗二叉树的大小,所以我们就利用这个性质去判断需要修改多少个节点,因为重量不同的节点所算出来的整颗二叉树点总重量必定也是不同的,所以我们只要搜索所有节点,然后去求每个节点所产生的总重,出现次数最多的总重量就代表有最多的叶子节点不需要进行修改。

三.总结

本题中希望用到这样一种数据结构,能够统计任意类型的数据的出现次数,我们可以采用stl里面的map,遍历和清空的使用方法如下所示,还有一个值得注意的地方就是给定一段字符的s和e,这段字符是一个数字,整理成数字的方法。

#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
map<long long,int>base;
int sum;
string str;
void dfs(int s,int e,int depth)
{
	if(str[s]=='[')
	{
		///cout<<s<<" "<<e<<endl;
		int p;
		p=0;
		for(int i=s+1;i<=e;i++)
		{
			//cout<<s<<" "<<e<<endl;
			if(str[i]=='[') p++;
			if(str[i]==']') p--;
			if(!p&&str[i]==',')
			{
				dfs(s+1,i-1,depth+1);
				dfs(i+1,e-1,depth+1);
			}
		}
	}
	else
	{
		long long  w;
		w=0;
		for(int i=s;i<=e;i++)
		{
			w=w*10+str[i]-'0';
		}
		//cout<<w<<endl;
		sum++;
		base[w<<depth]++;
		//printflag();
	}
}
int main()
{
	//freopen("input.txt","r",stdin);
	int T;
	cin>>T;
	for(int t=1;t<=T;t++)
	{

		cin>>str;
		int len;
		base.clear();
		sum=0;
		len=str.length();
		dfs(0,len-1,0);
		int maxn=0;
		for(map<long long,int>::iterator it=base.begin();it!=base.end();++it)
		{
			maxn=max(maxn,it->second);
		}
		printf("%d\n",sum-maxn);
	}
}
时间: 2024-08-25 22:24:08

UVa 12166(字符型二叉树的dfs)的相关文章

字符型变量

我们都知道,除了数字以外还有其他符号,比如字母等等,这些符号就是字符,存储字符的变量就是字符型变量. 一个字符型变量只能存储一个字符,比如time这个单词,就需要四个字符变量来存储. 那么,思考下面几个问题: 字符型变量在计算机内是以什么形式存储的? 答案很显然,也是0110这类的二进制码. 那么,二进制码是怎么变成字符的? 这个问题就比较专业了,就是用到ASCII码.简单地说,就是哪个数字代表哪个字符都统一标准.比如65就代表A,97代表a. 所以实际上,字符型就是个整数. 字符型的定义: c

再回首,Java温故知新(五):Java基本数据类型之字符型

Java作为一种强类型语言,意味着每一个变量都会有特定的类型,Java共有8种基本类型,其中有4种整型(byte.short.int.long).两种浮点型(float.double).1种字符型(char)和一种布尔型(boolean),值得注意的是,虽然字符串String非常常用,但是它却不属于基本类型. 三.字符型(接上篇浮点型) char类型用于表示单子字符,Unicode编码单元可以表示为十六进制,范围是从\u0000~\uffff,Java中的char类型使用16位的Unicode字

字符型弄详解

char和varchar分别称为定长和变长类型 对于定长char(N) 不论够不够长度,实际都占据N个长度 如果不够N个长度,用空格在末尾补到N个长度 而对于varchar不用空格补齐,但列内容前,有1-2个字节来标志该列的内容长度 char(M) 宽度在 0<=M<=255 varchar(M) 宽度在 0<=M<=65535 定长速度快些 mysql> create table test( mysql> ca char(6) not null default '',

基于python语言的tensorflow的‘端到端’的字符型验证码识别源码整理(github源码分享)

基于python语言的tensorflow的‘端到端’的字符型验证码识别 1   Abstract 验证码(CAPTCHA)的诞生本身是为了自动区分 自然人 和 机器人 的一套公开方法, 但是近几年的人工智能技术的发展,传统的字符验证已经形同虚设. 所以,大家一方面研究和学习此代码时,另外一方面也要警惕自己的互联网系统的web安全问题. Keywords: 人工智能,Python,字符验证码,CAPTCHA,识别,tensorflow,CNN,深度学习 2   Introduction 全自动区

【Java】Java_08 字符型与布尔值

1.字符型(2个字节) 单引号用来表示字符常量.例如‘A’是一个字符,它与“A”是不同的,“A”表示含有一个字符的字符串 char 类型用来表示在Unicode编码表中的字符 Unicode编码被设计用来处理各种语言的所有文字,它占2个字节,可允许有65536个字符:ASCII码占1个字节,可允许有128个字符,是Unicode编码表中前128个字符 char eChar = 'a'; char cChar ='中'; Unicode具有从0到65535之间的编码,他们通常用从’\u0000’到

字符型SQL注入

字符型SQL注入 很早就基于DVWA实现了字符型的SQL注入,但是一直感觉自己没有理解的特别清楚,这次又看了一下网上的一些讲解,试着总结一下.以下是我的一写浅薄见解,请大家批判着看. 基本原理 看看这条SQL语句 $query="select first_name from users where id='$_GET['id']'"; 这句SQL的语句就是基于用户输入的id在users表中找到相应的first_name,正常用户当然会输入例如1,2等等.但是如果有人输入这样的内容呢?

C语言中以十六进制输出字符型变量会出现&#39;ffffff&quot;的问题

最近在做一个C的嵌入式项目,发现在C语言中用printf()函数打印字符型变量时,如果想采用"%x"的格式将字符型变量值以十六进制形式打印出来,会出现一个小问题,如下: C代码   char buf[10] = {0}; buf[0] = 0xbf; printf("%2x\n\n\n", buf[0]);            /*在终端将会显示成:ffffffbf*/ buf[1] = 0x7f; printf("%2x\n\n\n", bu

让人又爱又恨的char(字符型)

今天来总结一下char型,平常写算法的时候对这个东西感觉都有一点绕着走,说到底还是对这部分的知识不熟悉所以有点怕他,不过以后不要怕,今天来总结一下 首先,说到字符型数据类型,char型,恩它是一种数据类型 然后,就是字符型变量 字符型变量的重点是将一个字符存入字符变量中,实际上存入的不是这个字符的形,而是这个字符的ASCII码 输入:getchar(); 输出:putchar(); getchar和scanf的区别在于他可以输入空格 字符型常量: 1.普通 'a' 2.转义 1.‘\t’ 斜杠+

如何为编程爱好者设计一款好玩的智能硬件(八)——LCD1602点阵字符型液晶显示模块驱动封装(中)

六.温湿度传感器DHT11驱动封装(下):如何为编程爱好者设计一款好玩的智能硬件(六)——初尝试·把温湿度给收集了(下)! 七.点阵字符型液晶显示模块LCD1602驱动封装(上):如何为编程爱好者设计一款好玩的智能硬件(七)——LCD1602点阵字符型液晶显示模块驱动封装(上) 八.LCD1602点阵字符型液晶显示模块驱动封装(中) 已经有好一阵子没写了,一方面是因为最近闲杂的事特多,另一方面(主要方面)是因为我卡在了LCD1602驱动的权衡上面——总共3个控制线和一个8位并行的数据线,放在51