九度 oj 题目1044:Pre-Post (确定树的状态)
来源:互联网 发布:淘宝商品价格怎么设置 编辑:程序博客网 时间:2024/05/04 16:06
http://ac.jobdu.com/problem.php?pid=1044
参考了
1.http://blog.csdn.net/yangnanhai93/article/details/40658571
2.http://www.acmerblog.com/jiudu-1044-2225.html
#include <stdio.h>#include <cstring>static int C[22][22];void getC(){ C[0][1] = C[1][1] = 1; for (int i = 2; i <=20; ++i) { C[0][i] = 1; for (int j = 1; j<=i; ++j) { C[j][i] = C[j-1][i-1] + C[j][i-1]; } } } int solve(char *pre, char* post,int m){ int len = (int) strlen(pre); if(len == 1) return 1; // only a root, status is unique pre = pre + 1; // remove the root post[len-1] = '\0'; //find sub-tree int count = 0; //number of sub-tree int res = 1; //number of tree-status while(*pre){ int i=0; char newPre[27],newPost[27]; while(pre[0]!=post[i]){ newPre[i] = pre[i]; newPost[i] = post[i]; i++; } newPre[i] = pre[i]; newPost[i] = post[i]; newPre[i+1] = '\0'; newPost[i+1] = '\0'; count++; pre = pre+i+1; post = post+i+1; res = res*solve(newPre,newPost,m); } res = res*C[count][m]; return res;} int main(){ //freopen("in/1044.in","r",stdin); int m; char pre[27],post[27]; getC(); while(scanf("%d %s %s",&m,pre,post) !=EOF){ printf("%d\n",solve(pre,post,m)); } }
0 0
- 九度 oj 题目1044:Pre-Post (确定树的状态)
- 九度OJ 题目1044:Pre-Post
- 【九度】题目1044:Pre-Post
- 九度 题目1044:Pre-Post
- 九度 题目1044:Pre-Post m元树问题
- 九度OJ 1044:Pre-Post(先序后序) (n叉树、递归)
- 题目1044:Pre-Post
- 题目1044:Pre-Post
- 题目1044:Pre-Post
- 题目1044:Pre-Post(递归的经典应用)
- 九度oj 题目1449:确定比赛名次
- 九度-1044-Pre-Post[分治和组合数]
- 九度 1044:Pre-Post 递归求n叉树结构个数
- 九度oj 题目1350:二叉树的深度
- 九度OJ—题目1088:剩下的树
- 九度OJ-题目1520:树的子结构
- 九度OJ-题目1521:二叉树的镜像
- 九度OJ-题目1350:二叉树的深度
- android studio使用记录
- 手机OCR
- 使用fastjson出现json中键值与属性名不一致
- 云端开发随思
- codevs 2806_红与黑_bfs
- 九度 oj 题目1044:Pre-Post (确定树的状态)
- svn常用命令,建账号,分组,查配置错误、关闭、重启
- 发送短信验证码到手机(阿里大于平台) java
- java.nio.ByteBuffer中flip、rewind、clear方法的区别
- Python学习--13 文件I-O
- Python学习--14 序列化
- java单例模式五种实现方式
- android遇到的问题
- C++类实现二叉树的构建和遍历