POJ 1240 Pre-Post-erous!(组合数学+递归)
来源:互联网 发布:linux 查看内存多大 编辑:程序博客网 时间:2024/05/16 16:22
Description
给出一棵m-叉树的前序和后序遍历序列,问这棵树有多少种
Input
多组用例,每组用例首先输入一个整数m表示树的叉数,之后为两个串表示该树的前序和后序遍历序列,以m=0结束输入
Output
对于每组用例,输出满足输入的前序和后序遍历序列的m-叉树的个数
Sample Input
2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda
0
Sample Output
4
1
45
207352860
Solution
根据前序遍历序列和后序遍历序列递归构造这棵树,递归过程中统计每个节点的儿子节点个数cnt,那么ans*=C(m,cnt)
Code
#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define maxn 33int m,id[maxn],ans;char pre[maxn],post[maxn];int C(int a,int b){ int ans=1; for(int i=1;i<=a-b;i++) ans=ans*(b+i)/i; return ans;}void count(int a,int b,int c,int d){ if(a>b)return ; int cnt=0; a++; int k=id[pre[a]-'a']; while(c<d) { cnt++; int l=k-c; count(a,a+l,c,k); c=k+1,a=a+l+1; k=id[pre[a]-'a']; } ans*=C(m,cnt);}int main(){ while(~scanf("%d",&m),m) { scanf("%s%s",pre,post); int len=strlen(pre); for(int i=0;i<len;i++) id[post[i]-'a']=i; ans=1; count(0,len-1,0,len-1); printf("%d\n",ans); } return 0;}
0 0
- POJ 1240 Pre-Post-erous!(组合数学+递归)
- poj 1240 Pre-Post-erous!(递归+组合计数)
- POJ 1240 Pre Post erous
- poj 1240 Pre-Post-erous!
- Poj Pre-Post-erous!
- poj 1240——Pre-Post-erous!
- poj 1240 Pre-Post-erous! 分治
- POJ 1240 Pre-Post-erous! 笔记
- poj1240 Pre-Post-erous!
- poj1240 Pre-Post-erous!
- Pre-Post-erous!
- POJ 1240 Pre-Post-erous! 前序+后序遍历判断树的形状的数量
- zoj 1500 Pre-Post-erous!
- zoj 1500 Pre-Post-erous!
- 【ZOJ 1500】 Pre-Post-erous!
- POJ 1240 Pre-Post-erous! 由前序后续遍历顺序推m-叉树的个数
- POJ1240 Pre-Post-erous! 【待完成】
- 题目1044:Pre-Post(递归的经典应用)
- Xutils3.0请求网络数据工具类
- Lua_第 11 章 数据文件与持久化
- keepalived配置文件详解
- 二叉树的建立,前序遍历,中序遍历,后序遍历
- JAVA Web开发过程中的路径问题
- POJ 1240 Pre-Post-erous!(组合数学+递归)
- 将秒转换为天,小时,分钟,秒。
- 十五、顺序执行动作重复动作
- 在数组中交换变量的陷阱
- 自定义对话框
- adb常用命令整理备忘录
- eclipse中jrebel安装+破解+配置
- 嵌入式linux:linux select函数与socket
- jQuery--jq对象和dom对象