UVA__12166 - Equilibrium Mobile(树)


Input
On the rst 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 dened 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
1 0 3

就是一个二元树,问你是否两边平衡;

这个比较题 大佬是这样想的,找到所有的儿子数,然后根据他的深度进行处理 ,   该点值*2*2*2~,寻找最大的重复数,用儿子数减去最大的重复数,即是 最小的改变数


#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<map>
typedef long long ll;
typedef unsigned long long llu;
const int MAXN = 1000000 + 10;
using namespace std;
char s[MAXN];
map<ll, int> mp;
map<ll, int> ::iterator it;
int cnt;//叶子结点总数
void dfs(int st,int et,int deep)
{
    if(s[st]=='[')
    {
        
        int p=0;
        for(int i=st+1;i<=et;i++)   
        {
            if(s[i]=='[') p++;
            if(s[i]==']')p--;
            if(!p&&s[i]==',')
            {
                dfs(st+1,i-1,deep+1);//处理前面的
                dfs(i+1,et-1,deep+1);//处理后面
            }
        }
    }
    else
    {
        cnt++;
        ll sum=0;
        for(int i=st;i<=et;i++)  //因为会有112 这种多位数的出现,所以要循环变数
        {
            sum=sum*10+s[i]-'0';
        }
        //cout<<" "<<sum<<endl;
        mp[sum<<deep]++;   //*2
    }
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s",s);
        int l=strlen(s);
        cnt=0;
        mp.clear();
        int ans=0;//重复数
        dfs(0,l-1,0);
        for(it=mp.begin();it!=mp.end();it++)
        {
            ans=max(ans,(it->second));
        }
        cout<<(cnt-ans)<<endl;
    }
    return 0;
}

     今天上午有点疯狂,不想学习,不想敲代码,没有理智,(本来是想去复习的),结果到北门骑着个小黄车就走了,骑到山边,听着歌傻了一路,但是到山边的时候特别爽,把歌关掉,听听鸟鸣,瞬间心情变好冷静了下了。然后转身回学校的时候,下坡,那是真的爽,非常爽,忍不住的放声高唱啦啦啦啦,瞬间心情变好,回来就去了机房,

话说大家最近很奇异,

惊!!!某高校ACM队集体思春!!!

怕是有毒哈哈哈,心情好了就好好敲代码啦,毕竟现在还是垃圾的一比,不能放松

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值