uva12166 遍历带括号的二叉树类型
来源:互联网 发布:笔记本cpu降温软件 编辑:程序博客网 时间:2024/06/06 13:55
#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<map>#include<cmath>using namespace std;string s;int cnt;map<long long,int> m;void dfs(int depth,int st,int en){ int p=0; if(s[st]=='[') { for(int i=st+1;i<en;i++) { if(s[i]=='[') p++; if(s[i]==']') p--; if(p==0&&s[i]==',') { dfs(depth+1,st+1,i-1);///递归查找左右子树 dfs(depth+1,i+1,en-1); } } } else { int n=0; for(int i=st;i<=en;i++) {/*cout<<s[i]<<endl;*/n=n*10+s[i]-'0';}///输出叶子结点的值 //cout<<"n:"<<n<<endl; int weight=n*pow(2,depth); cnt++; m[weight]++; }}int main(){ int n; scanf("%d",&n); while(n--) { int maxn=0,ans; m.clear(); cnt=0; cin>>s; dfs(0,0,s.length()-1); map<long long,int>::iterator it; for(it=m.begin();it!=m.end();it++) { //cout<<it->second<<endl; maxn=max(it->second,maxn); } //cout<<"c"<<cnt<<"m"<<maxn<<endl; ans=cnt-maxn; cout<<ans<<endl; }}这道题的思路挺巧妙的。先是从每个叶子节点 出发,让每个叶子节点作为整个天平的标准秤砣(即不用改变的),然后计算出每个标准秤砣得出来的整个天平的重量。然后如果不同秤砣使天平最终重量相等的话,即那几个标准秤砣是该天平达到该重量所不用改变的秤砣。求改变最小的秤砣使其平衡,实际就是求不用该变的秤砣的数量尽可能大。详情参考:http://blog.csdn.net/crazysillynerd/article/details/43876123
阅读全文
0 0
- uva12166 遍历带括号的二叉树类型
- 天平&二叉树--uva12166 Equilibrium Mobile
- 带栈的二叉树非递归先序遍历
- Java实现二叉树带行号的层序遍历
- 表达式转二叉树、计算带括号和浮点数的表达式
- Uva12166
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- Python·Jupyter Notebook的各种快捷键
- 用FPGA实现H.264/AVC硬件编解码器的构想
- pojThe merchant
- mac上安装opencv与python
- 基于S3C6410的DDR初始化
- uva12166 遍历带括号的二叉树类型
- bitbake yocto笔记
- BP神经网络在数据预测上的应用
- 利用栈实现两个一元多项式相加
- 第四周周中总结
- The Unique MST POJ
- JAVA 抽象类与接口
- 一个简单的C语言小程序,打印九九乘法表
- STL容器 erase的使用陷井