poj 1240 Pre-Post-erous!
来源:互联网 发布:淘宝网店转让怎么转 编辑:程序博客网 时间:2024/05/17 22:39
题目链接:http://poj.org/problem?id=1240
题目大意:给定一棵K叉树的先序遍历和后序遍历,问有多少种这样的树。因为字符串的每个字符都是不同的,所以可以确定一个根节点下有几个儿子和每棵子树所在的区间。用递归实现即可。
#include<stdio.h>#include<string.h>#include<string>using namespace std;int c[22][22];int m;char str1[30],str2[30];void init(){int i,j;for(i=0;i<22;i++)c[i][0]=1;for(i=0;i<22;i++)c[i][i]=1;for(i=1;i<22;i++)for(j=1;j<i;j++){c[i][j]=c[i-1][j-1]+c[i-1][j];//printf(" i %d j %d c%d\n",i,j,c[i][j]);}}int dfs(int st1,int ed1,int st2,int ed2){//printf("co\n");int tmp,i;int rec=1;int num=0;if(ed1-st1==-1)return 1;while(st1<=ed1){for(i=st2;i<=ed2;i++){if(str2[i]==str1[st1]){tmp=i;num++;break;}}//printf("st1 %d ed1 %d tmp %d \n",st1,ed1,tmp);rec*=dfs(st1+1,st1+tmp-st2,st2,tmp-1);st1=st1+tmp-st2+1;st2=tmp+1;}return rec*c[m][num];}int main(){init();int len;int rec;while(scanf("%d",&m),m){scanf("%s%s",str1,str2);len=strlen(str1);//printf("a");rec=dfs(1,len-1,0,len-2);//printf("kkkk\n");printf("%d\n",rec);}return 0;}
- 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! 笔记
- 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! 【待完成】
- poj1240Pre-Post-erous!
- 工具C-kermit
- String,StringBuffer与StringBuilder
- asp.net核心对象
- 平面点集凸包算法线性复杂度实现
- Hibernate中的session.load(class,id)参数问题
- poj 1240 Pre-Post-erous!
- 安全网站
- ios屏幕旋转注意事项
- 对日志创建处理下载为excel格式的文件续(1)(
- DOM方式解析XML文档(另2种方式为SAX,STAX),(第二种直接用sax方式用java类实现)
- V4l2 Capture Sequence
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- MySQL判断字段值为null
- BroadCast广播优先级问题