1412 the abundance of Binary Trees

Source: Internet
Author: User

 

Description

The richness of a binary tree is defined as the product of the number of points at each layer and the height of the tree. For calculation convenience, the height of the root is 0. for example, the abundance of tree A (B (D,), C (,) is 1 × 0 + 2 × 1 + 1 × 2 = 4 (5)

Input

The input contains N + 1 rows. The number of groups of the first behavior test data n. The N rows below are the N Binary Trees represented by generalized tables, and the elements in the tree are represented by char.

Output

A total of N rows are output, corresponding to N groups of test data, and the richness of the tree is output.

Sample Input
1
A(B(D,),C(,))
Sample output
4

 

Array Simulation

#include <stdio.h>#include<string.h>main(){int number;int length;int a[100];char s[1000];int i;int up;int sum;int max;scanf("%d",&number);while(number--){up=max=0;sum=0;memset(a,0,sizeof(a));scanf("%s",&s);length=strlen(s);for(i=0;i<length;i++){if(s[i]!='('&&s[i]!=')'&&s[i]!=','){  a[up]++;}if(s[i]=='('){  up++;max++;}if(s[i]==')')up--;}for(i=0;i<=max;i++)             sum+=a[i]*i;printf("%d\n",sum);}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.