POJ1240
来源:互联网 发布:淘宝客怎么查看佣金 编辑:程序博客网 时间:2024/05/22 11:56
Description
a a a a / / \ \ b b b b / \ / \c c c c
All of these trees have the same pre-order and post-order traversals. This phenomenon is not restricted to binary trees, but holds for general m-ary trees as well.
Input
m s1 s2
indicating that the trees are m-ary trees, s1 is the pre-order traversal and s2 is the post-order traversal.All traversal strings will consist of lowercase alphabetic characters. For all input instances, 1 <= m <= 20 and the length of s1 and s2 will be between 1 and 26 inclusive. If the length of s1 is k (which is the same as the length of s2, of course), the first k letters of the alphabet will be used in the strings. An input line of 0 will terminate the input.
Output
Sample Input
2 abc cba2 abc bca10 abc bca13 abejkcfghid jkebfghicda0
Sample Output
4145207352860
这个题也比较简单,写的开心
#include <iostream>#include <cstring>#include <stdio.h>using namespace std;int m,sum;char s1[25],s2[25];int fd(int lpre,int rpre,int lpost,int rpost){ if(lpre==rpre) return 1; int cou,nr[20],nl[20],i,j,k,ve,to; k=1; i=lpre+1; j=lpost; cou=0; nr[0]=j-1; nl[0]=i-1; while(i<=rpre) { char tem=s1[i]; //cout<<i<<endl; for(;j<=rpost-1;j++) { i++; if(tem==s2[j]) { cou++; nl[k]=i-1; nr[k]=j; k++; j++; //cout<<"sdsa"<<endl; break; } } } ve=1; for(i=0;i<cou;i++) ve=ve*(m-i)/(i+1); //cout<<cou<<endl<<ve<<endl; to=1; for(i=0;i<cou;i++){ //cout<<nl[i]+1<<"-"<<nl[i+1]<<endl; //cout<<nr[i]+1<<"-"<<nr[i+1]<<endl; to=to*fd(nl[i]+1,nl[i+1],nr[i]+1,nr[i+1]); } return to*ve;}int main(){ cin>>m>>s1>>s2; int lpre,rpre,lpost,rpost,len; while(m!=0) { len=strlen(s1); lpre=0;rpre=len-1; lpost=0;rpost=len-1; cout<<fd(lpre,rpre,lpost,rpost)<<endl; cin>>m>>s1>>s2; } return 0;}
- poj1240
- POJ1240
- 2011.12.26 POJ1240
- poj1240 Pre-Post-erous!
- poj1240 Pre-Post-erous!
- POJ1240 Pre-Post-erous! 【待完成】
- 2016冬天学习记录-1.14
- 【博客已转移,新博客地址】
- Dubbo点滴(2)之集群容错
- AJAX异步请求
- springboot的缓存技术
- POJ1240
- 关于屏幕设配相关
- Android 判断SD卡是否存在及容量查询
- (HDU 5774)Where Amazing Happens <水题> 多校训练4
- 【J2SE】异常与异常处理
- 数据结构实验之链表八:Farey序列
- 罗马共和国最后的日子以及罗马帝国的灭亡
- VI的基本编辑命令-复制和粘贴(4)
- 记录生活&学习