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);}}